Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: style guidance

by JavaFan (Canon)
on Nov 08, 2009 at 00:53 UTC ( [id://805704]=note: print w/replies, xml ) Need Help??


in reply to Re: style guidance
in thread style guidance

Don't assign to a global variable without localising it. You just clobbered your parent's data.
Of course, the parent could protect its $_ my using a lexical variable. And so can do_stuff1. local $_ is in idiom I've had no need for the past 2 years.
sub do_stuff1 { my $_ = shift; chomp; say; }
You also wrote:
or even safer:
sub do_stuff1 { local *_; $_ = shift; ... }
Safer? You should test your code if you label it 'safe', or 'safer'. I guess you didn't realize that local *_; also gives you a new value for @_, so you no longer have access to the subs parameters?

Replies are listed 'Best First'.
Re^3: style guidance
by ikegami (Patriarch) on Nov 08, 2009 at 01:56 UTC

    You seem to imply it's not safer. Having a bug doesn't make it unsafe, it's makes it buggy.

    You're also wrong that I don't realize @_ also gets affected. It just slipped through the cracks even though I remembered while I was composing the post. I usually use local * to do aliasing where this isn't a problem. I never localize $_ because it's suppose to be a shortcut but ends up longer.

    Fixed.

    As for my $_, that's new to the current version. Most people who come here don't use the latest version of Perl yet. I was going for a working solution, not a theoretical one. Thanks for mentioning it as an alternative, though.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2025-11-13 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (68 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.