m// and binding Operator in Perl

How to use m// and binding Operator?

Explanation

m// Operator:


The pattern to be matched for is placed in between the // slashes of "m//" operator, if any additional options are to be placed it can be added after the second forward slash like "m/***/s".

Quoted strings, you may use other symbols in place of //. However, if you use //, you may omit the prefix m.In case of heavily slashed patterns one can use other symbols like "|".

Example :


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
if ("Capable" =~ m/able/)
{
print "match!n";
}
else
{
print "no match!n";
}
Result :

match!

In the above example the pattern "able" is checked in the string "capable" to find whether its present, since it exists it returns the result "match!".

Bind Operators:


If an expression combined using the "=~" or "!~" binding operators,that expression is searched for in the pattern specified. If the binding operator is missing "=~" is assumed as the binding operator and "$" is taken as the expression to be searched for.

In scalar context, the binding operator "=~" returns a true value if the expression matches the pattern, an empty string if it returns a false."!~" simply inverts so that if the expression matches the pattern a false value is returned, a true value otherwise. Therefore, the following two expressions are equivalent:

!($expression =~ m/pattern/)
$expression !~ m/pattern/

Ask Questions

Ask Question