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

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

by ikegami (Pope)
on May 11, 2018 at 20:18 UTC ( #1214403=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2018-11-14 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (163 votes). Check out past polls.

    Notices?