Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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 =~ ?
Replies are listed 'Best First'.
Re: Re: = rather than =~ ?
by tachyon (Chancellor) on Jan 02, 2004 at 03:17 UTC
    my $foo = m/(foo)/ ? $1 : '';



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?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2016-05-29 22:42 GMT
Find Nodes?
    Voting Booth?