Binding Operators
Binary =~ binds a string expression to a pattern match, substitution, or
transliteration (loosely called translation). These operations would
otherwise search or modify the string contained in $_ (the default variable). The string you want
to bind is put on the left, while the operator itself is put on the right.
The return value in scalar context generally indicates the success or
failure of the operator on the right, since the binding operator doesn’t
really do anything on its own. The exception to this is when using the
/r modifier with substitution (s///) or transliteration (y///, tr///),
which returns a copy of the modified string. Behavior in list context
depends on the particular operator.
If the right argument is an expression rather than a pattern match,
substitution, or transliteration, it will be interpreted as a search
pattern at runtime. That is to say, $_ =~
$pat is equivalent to $_ =~
/$pat/. This is less efficient than an explicit search, since
the pattern must be checked and possibly recompiled every time the
expression is evaluated. You can avoid this recompilation by precompiling
the original pattern using the qr//
(quote regex) operator.
Binary !~ is just like =~ except the return value is negated logically.
Binary “!~” that attempts to use the /r
modifier for a nondestructive substitution or transliteration is a syntax
error. Apart from that, the following expressions are functionally
equivalent:
$string !~ /pattern/ !( $string =~ /pattern/ ) ...
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