If you read perldoc perlretut, this is an extract that is useful to your understanding.
· s modifier (//s): Treat string as a single long line. '.' matches any character, even "\n". "^" matches only at the beginning of the string
and "$" matches only at the end or before a newline at the end.
· m modifier (//m): Treat string as a set of multiple lines. '.' matches any character except "\n". "^" and "$" are able to match at the start
or end of any line within the string.
· both s and m modifiers (//sm): Treat string as a single long line, but detect multiple lines. '.' matches any character, even "\n". "^" and
"$", however, are able to match at the start or end of any line within the string.
Here are examples of "//s" and "//m" in action:
$x = "There once was a girl\nWho programmed in Perl\n";
$x =~ /^Who/; # doesn't match, "Who" not at start of string
$x =~ /^Who/s; # doesn't match, "Who" not at start of string
$x =~ /^Who/m; # matches, "Who" at start of second line
$x =~ /^Who/sm; # matches, "Who" at start of second line
$x =~ /girl.Who/; # doesn't match, "." doesn't match "\n"
$x =~ /girl.Who/s; # matches, "." matches "\n"
$x =~ /girl.Who/m; # doesn't match, "." doesn't match "\n"
$x =~ /girl.Who/sm; # matches, "." matches "\n"
Most of the time, the default behavior is what is wanted, but "//s" and "//m" are occasionally very useful. If "//m" is being used, the start of
the string can still be matched with "\A" and the end of the string can still be matched with the anchors "\Z" (matches both the end and the
newline before, like "$"), and "\z" (matches only the end):
$x =~ /^Who/m; # matches, "Who" at start of second line
$x =~ /\AWho/m; # doesn't match, "Who" is not at start of string
$x =~ /girl$/m; # matches, "girl" at end of first line
$x =~ /girl\Z/m; # doesn't match, "girl" is not at end of string
$x =~ /Perl\Z/m; # matches, "Perl" is at newline before end
$x =~ /Perl\z/m; # doesn't match, "Perl" is not at end of string