[Melbourne-pm] Perl switch statements

Michael G Schwern schwern at pobox.com
Tue Oct 23 16:54:55 PDT 2012


On 2012.10.23 3:25 PM, Ryan, Martin G wrote:
> Thank you for expanding on that - I found it very illuminating.
> Fascinating how if it was previously declared with "my", it localizes
> a lexical variable. (which you can't do normally, yes?)

You're right that you can't localizing a lexical variable.  What really
happens is foreach forces the global (and localized) $_ to shadow your lexical
$_.  Sort of like this:

    $ perl -wle 'my $foo = 23; { our $foo; local $foo = 42; }  print $foo'
    23

    $ perl -wle 'my $_ = 23; print $_; for(1..3) { print $_ } print $_'
    23
    1
    2
    3
    23


> I've always used a fresh variable for the cause - "$i" if I'm running low on
> imagination - and hence the question doesn't arise (probably best that way for
> the sanity of future maintainers).

This is a good idea.  $_ has so many side effects associated with it if you
have the option to avoid it do so.


-- 
Anyway, last I saw him, the TPF goons were pouring concrete around him,
leaving only one hole each for air, tea, and power.  No ethernet,
because he's using git.
    -- Eric Wilhelm on one of my disappearances


More information about the Melbourne-pm mailing list