Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binding operator =~ explanation 2

Status
Not open for further replies.

sm42

Technical User
Dec 11, 2003
133
GB
Hello

I'm reading and learning about Perl and have a basic question.

I'm not sure if Im being a bit silly...

I have searched google for a basic:simple explanation to no avail...


"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 indicates the success or failure of the operator on the right, since the binding operator doesn't really do anything on its own.

If the right argument is an expression rather than a pattern match, substitution, or transliteration, it will be interpreted as a search pattern at run time. 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."

Hey!!!!!
this link is ok, but the Penny does not drop for me ...

can someone please explain what =~ is for

thanks
 
Perhaps you just need it in plain language:

Normally regexes take the contents of the $_ variable as input. Often this is what you want if you are reading a file, for example
Code:
# find all the *.jpegs
while (<>) {
   print if (/\.jpeg$/);
}
If you want the regex to work on the contents of another variable instead, you use the binding operator
Code:
# multi-fred application
foreach my $item (@items) {
   print "$item\n" if ($item =~ /fred/);
}

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
That has to be one of the simplest multi-fred'ed applications ever..

( I'll get my coat... )
 
thanks and sorry for the late reply
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top