Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Perl::Critic says don't modify $_ in list functions and other things

by haukex (Bishop)
on Jul 10, 2020 at 19:34 UTC ( #11119157=note: print w/replies, xml ) Need Help??


in reply to Re: Perl::Critic says don't modify $_ in list functions and other things
in thread Perl::Critic says don't modify $_ in list functions and other things

subs using $_ should localize *_, which replaces the entire glob

Since this only about $_, then local $_; is better, because localizing @_ is unlikely to be in the sub's interest. (Update: Except on Perl versions before 5.14, as per the replies.)

Replies are listed 'Best First'.
Re^3: Perl::Critic says don't modify $_ in list functions and other things
by jcb (Vicar) on Jul 10, 2020 at 23:40 UTC

    Using local $_ can fail if $_ is currently aliased to something read-only. The "Localization of special variables" and "Localization of globs" sections in perlsub explained this and recommended using local *_ instead in the affected versions. You can avoid problems with also localizing @_ by unpacking your arguments into lexicals before localizing *_.

    Beginning with perl 5.14, local $_ is a special case that also strips any magic associated with $_, while preserving the other slots in *_. Since Lady_Aleena mentions that her Web host is using a perl older than 5.14, this is a concern for our questioner.

      Beginning with perl 5.14, local $_ is a special case

      Ah, I see, you're referring to RT#84774. Thanks for clarifying.

      Update: Since the OP is no longer on 5.8.8, I think it's appropriate to mention that the local *_ workaround is no longer necessary.

        Fair enough; but her signature block still said that her Web host was on 5.8.8 when she posted the question and she did not clarify here until after we had had that discussion.

        While our questioner may no longer need to be concerned about that detail, the issue is still a concern for module authors (and really only module authors — scripts can simply avoid aliasing $_ to read-only values when calling such "sensitive" routines) targeting backwards compatibility to perl versions before 5.14, and the workaround works in later versions, albeit with the caveat that you must collect your arguments into lexicals before localizing *_ or only use them again after exiting the block in which *_ was localized. I generally use the my $foo = shift; my $bar = shift; idiom to convert arguments to independent variables instead of leaving them as aliases, so localizing *_ is not a problem — @_ is empty well before that point.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2021-01-17 23:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?