Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does =~ do in Perl? [closed]

Tags:

operators

perl

I guess the tag is a variable, and it is checking for 9eaf - but does this exist in Perl?

What is the "=~" sign doing here and what are the "/" characters before and after 9eaf doing?

if ($tag =~ /9eaf/) {     # Do something } 
like image 434
Invictus Avatar asked Apr 04 '12 20:04

Invictus


People also ask

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/) {

What is the meaning of $1 in Perl regex?

$1 equals the text " brown ".

What is $/ in Perl?

From the Perl Doc. $/ and $\ which are the input and output record separators respectively. They control what defines a "record" when you are reading or writing data. By default, the separator used is \n .


1 Answers

=~ is the operator testing a regular expression match. The expression /9eaf/ is a regular expression (the slashes // are delimiters, the 9eaf is the actual regular expression). In words, the test is saying "If the variable $tag matches the regular expression /9eaf/ ..." and this match occurs if the string stored in $tag contains those characters 9eaf consecutively, in order, at any point. So this will be true for the strings

9eaf  xyz9eaf  9eafxyz  xyz9eafxyz 

and many others, but not the strings

9eaxxx 9xexaxfx 

and many others. Look up the 'perlre' man page for more information on regular expressions, or google "perl regular expression".

like image 110
jmhl Avatar answered Oct 02 '22 16:10

jmhl