Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by YenForYang (Beadle)
on May 11, 2018 at 19:54 UTC ( #1214399=perlquestion: print w/replies, xml ) Need Help??
YenForYang has asked for the wisdom of the Perl Monks concerning the following question:

I was looking through the source code of Find.pm of File::Find. The comments mention why local()-izing is needed, which I kinda-sorta understand since the the callback functions passed in may call find or finddepth again, so local essentially makes a copy of the global variables (am I right or wrong here?). However, I don't understand why local *_ isn't sufficient. Why is set to \my $a? Why \my $a in particular and not some other variable--I mean, could any variable substitute \my $a? Thanks.

Replies are listed 'Best First'.
Re: What is the purpose of local *_ = \my $a in File::Find?
by ikegami (Pope) on May 11, 2018 at 20:18 UTC

    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.

      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: perlquestion [id://1214399]
Approved by Perlbotics
Front-paged by haukex
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2018-05-21 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?