http://www.perlmonks.org?node_id=1214403


in reply to What is the purpose of local *_ = \my $a in File::Find?

local $_ could be used to localize $_, but that doesn't remove any magic associated with it on some versions of Perl.

$ perl -wE'for ($|) { local $_; $_ = "abc"; say; }' Argument "abc" isn't numeric in scalar assignment at -e line 1. 0

local *_ would do the trick by forcing an entirely new $_ to be created, but that also gets rid of @_, %_, etc.

$ perl -E'$_="x"; @_=(4,5); say $_,@_; local *_; say $_,@_;' x45

Assigning to a glob is weird, though. It only assigns to the relevant slot. So that means assigning a reference to a scalar to *_ only replaces $_

$ perl -E'$_="x"; @_=(4,5); say $_,@_; local *_ = \my $a; say $_,@_;' x45 45

Any fresh scalar would have worked.

Note that this aliases $_ to the scalar, but File::Find does not take advantage of that feature.

Replies are listed 'Best First'.
Re^2: What is the purpose of local *_ = \my $a in File::Find?
by YenForYang (Beadle) on May 16, 2018 at 05:27 UTC

    Awesome, thanks for the explanation.

    What do you mean though by

    Note that this aliases $_ to the scalar, but File::Find does not take advantage of that feature.
    Specifically the part about not taking advantage of the feature. What features could be taken advantage of? Thanks

    EDIT:

    Oh wait are you saying that $a could be used like $_ (they become equivalent?). Or is there something else?

      $ perl -E'local *_ = \my $s; $_ = 123; say $s; $s = 456; say $_;' 123 456