Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^4: = rather than =~ ? (too brief?)

by Coruscate (Sexton)
on Jan 02, 2004 at 05:43 UTC ( #318248=note: print w/ replies, xml ) Need Help??


in reply to Re^3: = rather than =~ ? (too brief?)
in thread = rather than =~ ?

$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 :)


Comment on Re^4: = rather than =~ ? (too brief?)
Select or Download Code
Actually, it's worse than that
by mrpeabody (Friar) on Jan 02, 2004 at 19:44 UTC
    One should never, ever write "my $foo = 1 if /(foo)/;". From perlsub:

    NOTE: The behaviour of a "my" statement modified with a statement modifier conditional or loop construct (e.g. "my $x if ...") is undefined. The value of the "my" variable may be "undef", any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.
Re: Re^4: = rather than =~ ? (too brief?)
by BrowserUk (Pope) on Jan 02, 2004 at 20:31 UTC

    I think I understand what you/they mean, but the empirical evidence says different.

    p:\test>perl -Mstrict -lwe"my $foo=1 if 'X' =~ /Y/; print 'Result is: +', $foo; " Use of uninitialized value in print at -e line 1. Result is: p:\test>perl -Mstrict -lwe"my $fob=1 if 'X' =~ /Y/; print 'Result is: +', $foo; " Global symbol "$foo" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.

    In both examples, $foo in the print statement has never been assigned a value.

    In the first example, the compiler has seen the declaration and so only it's definedness is a problem and we get the "Use of uninitialized value" warning.

    In the second case, it is not only undefined, it doesn't exists and so we get the "Global symbol "$foo" requires explicit package name" error.

    It clearly indicates that although the if condition is false and the assignment is never performed, the my declaration has been see and acted upon.

    This is true with 5.6.1 and 5.8.1. Maybe the documentation is out of date?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!

Re^5: = rather than =~ ? (too brief?)
by Aristotle (Chancellor) on Jan 02, 2004 at 22:00 UTC
    No, that is wrong. Satisfying strict depends only on the compile-time effect of my, for which its mere presence suffices. However, a conditional prevents its run-time effect of allocating a new scalar for that lexically scoped name, so that the name remains associated with the previously used variable. Observe:
    use strict; use warnings; sub remember { my $memory if 0; print "Previous value of \$memory: $memory\n"; $memory = shift; } remember "foo"; remember 42; remember "bar"; remember; __END__ Use of uninitialized value in concatenation (.) or string at - line 6. Previous value of $memory: Previous value of $memory: foo Previous value of $memory: 42 Previous value of $memory: bar
    This if 0 hack has been known for a long time and might even be useful if it weren't such a dirty trick. An actual condition in that place nearly certainly means that someone is going to be very surprised.

    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-09-23 07:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (211 votes), past polls