What does =~ mean in Perl?

55,620

=~ 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";
}
Share:
55,620
Cole Tobin
Author by

Cole Tobin

Hi, I'm Cole.

Updated on July 16, 2022

Comments

  • Cole Tobin
    Cole Tobin almost 2 years

    Possible Duplicate:
    What does =~ do in Perl?

    In a Perl program I am examining (namly plutil.pl), I see a lot of =~ on the XML parser portion. For example, here is the function UnfixXMLString (lines 159 to 167 on 1.7 ($VERSION wrongly declared as "1.5")):

    sub UnfixXMLString {
        my ($s) = @_;
    
        $s =~ s/&lt;/</g;
        $s =~ s/&gt;/>/g;
        $s =~ s/&amp;/&/g;
    
        return $s;
    }
    

    From what I can tell, its C prototype would be (C-like) string UnfixXMLString(string s), and it uses the =~ operator on the parameter (s) and then returns the modified string, but what is it doing?