Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2018-02-19 00:50 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (257 votes). Check out past polls.