in reply to non aliased default var $_

I've been using the following for over a decade:
my @bla = map {my $_ = $_; tr/h//d; $_} @arr;
Of course, on older Perls, the 'my' was spelled 'local'.

Replies are listed 'Best First'.
Re^2: non aliased default var $_
by LanX (Saint) on Mar 10, 2010 at 13:42 UTC
    Thanx, my $_ = $_; is really a good idiom!

    In this example with map it might look complicated, because we have to define a return value...

    ...but in a for loop it's easy to spot, understand and handy to use.

    Perfect! :)

    Cheers Rolf

      is really a good idiom!

      For modern perl (5.10+)

        from perlsub

        The fix is to add local():

        local $_ = 5;

        ...

        As of perl 5.9.1, you can now use a lexical version of $_ by declaring it in a file or in a block with "my".

        Cheers Rolf