What does =~ mean in Perl?

=~ is the Perl binding operator. It's generally used to apply a regular expression to a string; for instance, to test if a string matches a pattern:

if ($string =~ m/pattern/) {

Or to extract components from a string:

my ($first, $rest) = $string =~ m{^(\w+):(.*)$};

Or to apply a substitution:

$string =~ s/foo/bar/;

=~ is the Perl binding operator and can be used to determine if a regular expression match occurred (true or false)

$sentence = "The river flows slowly.";
if ($sentence =~ /river/)
{
    print "Matched river.\n";
}
else
{
    print "Did not match river.\n";
}

Tags:

Perl

Operators