Home: Perl Programming Help: Beginner:
slashes and dollar



boonflies
New User

May 3, 2017, 6:20 AM


Views: 1195
slashes and dollar

I am learning perl using a program. I came across a piece of code,

if ($a =~ /MN_C\.o$/)

can it be stripped down to understand the meaning of backslash, forward slash and dollar


BillKSmith
Veteran

May 3, 2017, 8:34 AM


Views: 1190
Re: [boonflies] slashes and dollar

Your statement is a conditional statement that takes the 'true' branch if the string in $a ends with the substring 'MN_C.o'. Lets take it apart. The '$' tells us that the variable $a is a scalar. The operator '=~' tells us that $a must match the following regular expression. (This tells us that $a will be interpreted as string rather than as a number.) The forward slashes mark the beginning and end of the regular expression. Most characters in a regular expression represent themselves. In your example, the '\', '.', and '$' are special. The '$' requires the match to occur at the end of the string. The '\' is called an escape character. It means that the following character (the '.' in this case) loses its special meaning and merely represents itself.
Good Luck,
Bill