What does = ~ mean in Perl?

Possible duplicate:
What does ~ ~ do in Perl?

In the Perl program I'm learning (namly plutil.pl ), I see a lot of =~ in the XML parser part. For example, here is the UnfixXMLString function (lines 159 to 167 in 1.7 ( $VERSION erroneously 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 say, his C prototype will be (C-like) string UnfixXMLString(string s) , and he uses the =~ operator for parameter ( s ) and then returns the modified string, but what does he do?

+9
operators perl
source share
2 answers

=~ is the Perl binding operator. It is usually used to apply a regular expression to a string; for example, check if the string matches the pattern:

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

Or to extract components from a string:

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

Or apply a replacement:

 $string =~ s/foo/bar/; 
+20
source share

=~ is a Perl binding operator and can be used to determine whether a regular expression matches (true or false)

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

All Articles