Perl m Function



This match operator is used to match any keyword in given expression. Parentheses after initial m can be any character and will be used to delimit the regular expression statement.

Regular expression variables include $, which contains whatever the last grouping match matched; $&, which contains the entire matched string; $`, which contains everything before the matched string; and $', which contains everything after the matched string.


Following is the simple syntax for this function −


Return Value

This function returns 0 on failure and 1 on success,


Following is the example code showing its basic usage −

#!/usr/bin/perl -w

$string = "The food is in the salad bar";
$string =~ m/foo/;
print "Before: $`\n";
print "Matched: $&\n";
print "After: $'\n";

When above code is executed, it produces the following result −

Before: The 
Matched: foo
After: d is in the salad bar