The m// Operator (Matching)
m/PATTERN/modifiers/PATTERN/modifiers?PATTERN?modifiers(deprecated)EXPR=~ m/PATTERN/modifiersEXPR=~ /PATTERN/modifiersEXPR=~ ?PATTERN?modifiers(deprecated)
The m// operator searches the string in the scalar
EXPR for PATTERN.
If / or ? is the delimiter, the initial m is optional. Both ? and '
have special meanings as delimiters: the first is a once-only match; the
second suppresses variable interpolation and the seven translation escapes
(\U and company, described
later).
If PATTERN evaluates to a null string,
either because you specified it that way using // or because an interpolated variable
evaluated to the empty string, the last successfully executed regular
expression not hidden within an inner block (or within a split, grep, or map) is used instead.
In scalar context, the operator returns true (1) if successful, false ("") otherwise. This form is usually seen in
Boolean context:
if ($shire =~ m/Baggins/) { ... } # search for Baggins in $shire
if ($shire =~ /Baggins/) { ... } # search for Baggins in $shire
if ( m#Baggins# ) { ... } # search right here in $_
if ( /Baggins/ ) { ... } # search right here in $_Used in list context, m//
returns a list of substrings matched by the capturing parentheses in the
pattern (that is, $1, $2, $3, and
so on), as described later under Grouping and Capturing. The numbered variables are still set even when the list is returned. If the match fails in list context, a null list is returned. If the match succeeds ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access