Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: = rather than =~ ?

by Anonymous Monk
on Jan 01, 2004 at 17:17 UTC ( #318132=note: print w/ replies, xml ) Need Help??


in reply to = rather than =~ ?

It depends. I don't know what $input is, but that code is matching against $_. If the match fails, the match fails. Such code should always be written something like $foo = $1 if /(foo)/;


Comment on Re: = rather than =~ ?
Re: Re: = rather than =~ ?
by tachyon (Chancellor) on Jan 02, 2004 at 03:17 UTC
    my $foo = m/(foo)/ ? $1 : '';

    cheers

    tachyon

Re: Re: = rather than =~ ?
by ysth (Canon) on Jan 02, 2004 at 04:11 UTC
      If you feel the need point that one out, you should probably also explain why that is.. :)

      Makeshifts last the longest.

        $foo = $1 if /(foo)/; may very well be the expected behaviour in certain situations where we want to modify $foo if the match is successfull or leave it otherwise. Using it when declaring a variable is not desirable however. If there is no match, then $foo will not be declared and any attempt to read the scalar later will result in a "variable not declared" error. Of course, this is why we use strict;. Without it, we won't get an error :) defined.

        Basically, $foo = $1 if /(foo)/; should really only be used where we might otherwise do a $foo = /(foo)/ ? $1 : $foo; (which is just programatically ugly).

        Update: Whoa, was I off base. Read the replies to this node to see the reason :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://318132]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2014-07-13 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (244 votes), past polls