Beefy Boxes and Bandwidth Generously Provided by pair Networks Ovid
P is for Practical
 
PerlMonks

Don't local(@_)

by simonm (Vicar)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Jan 13, 2002 at 05:23 UTC ( #138370=perlmeditation: print w/ replies, xml ) Need Help??

An interesting discovery about local has emerged in the course of debugging Class::MakeMethods (thanks to an excellent bug report from Adam Spiers).

Take a moment to predict the output of the code below before you run it.

#!/usr/bin/perl -w use strict; print_args( 123, 456, 789 ); sub print_args { tamper_args( 'All your arguments are belong to us.' ); print "My arguments were: " . join(', ', @_) . "\n"; } sub tamper_args { local @_ = ( 'This is not a potato.' ); }

On my machine (perl v5.6.1 for darwin), the call to tamper_args somehow clobbers the argument list visible to print_args, producing the following startling result: "My arguments were: All your arguments are belong to us."

This appears to be a conflict between the restoration of local values at the end of a sub {...} scope and the restoration of @_ that always happens at the end of a sub. In particular, the problem goes away if the local is inside another { } block (presumably because the two scope-exits are handled separately). It also goes away if tamper_args is called without a parenthesized argument list (presumably because there's no implicit local-ization in that case.

Now, I'll grant that there's generally no need to local @_ like this, but I've also not been able to find anything that says you're not allowed to do so -- perhaps this consitutes an actual Perl bug?

Comment on Don't local(@_)
Download Code
Re: Don't local(@_)
by blakem (Monsignor) on Jan 13, 2002 at 06:46 UTC
    Ouch! That looks like a bug to me... (unless I'm misunderstanding some deep voodoo) BTW 5.00503 and 5.6.1 behave differently on linux (though neither behave as I would expect...):
    % perl5.00503 tamper.pl My arguments were: % perl5.6.1 tamper.pl My arguments were: All your arguments are belong to us. % perl5.6.0 tamper.pl My arguments were: All your arguments are belong to us.
    Update: Added 5.6.0 test from a different linux machine...
    Update2: You might try playing around with local *_ which doesn't do the right thing, but at least it does something different....

    -Blake

[reply]
[d/l]
[select]
Re: Don't local(@_)
by hossman (Parson) on Jan 13, 2002 at 06:50 UTC
    This definitely strikes me as a bug. Per Programming Perl...
    If you have a local that looks like this:
    { local $var = $newvalue; some_func(); ... }
    you can think of it purely in terms of run-time assignments:
    { $oldvalue = $var; $var = $newvalue; some_func(); ... } continue { $var = $oldvalue; }
    The difference is that with local the value is restored no matter how you exit the block, even if you prematurely return from that scope.

    Regardless of or what variable you are local'ing in your sub, it should return it to it's orriginal value on exit -- and it certainly shouldn't affect the callers scope.

[reply]
[d/l]
[select]
Re: Don't local(@_)
by robin (Chaplain) on Jan 13, 2002 at 23:35 UTC
    I agree with the previous posters that this is definitely a bug of some sort. Whether it ought to work, I'm less sure: perhaps it should just be forbidden to localise @_.

    I tried your test code on a fairly recent development snapshot of perl, and the bug is still there. You should perlbug it. (Run the perlbug command and follow the instructions.)

[reply]
Re: Don't local(@_)
by dmmiller2k (Chaplain) on Jan 14, 2002 at 20:39 UTC

    Nice catch! Definitely looks like a bug.

    Although I'm having trouble envisioning under what circumstances this would create a problem in production code (I don't remember ever having a reason to localize @_), it's still a good catch. .

    dmm

    If you GIVE a man a fish you feed him for a day
    But,
    TEACH him to fish and you feed him for a lifetime
[reply]
[d/l]

Back to Meditations


Login:
Password
remember me
What's my password?
Create A New User

Node Status
node history
Node Type: perlmeditation [id://138370]
Approved by root
help
Community Ads
Chatterbox
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users
Others drinking their drinks and smoking their pipes about the Monastery: (8)
GrandFather
wfsp
herveus
Spooty
Eyck
cpc
vishi83
gnosti
As of 2009-11-21 09:16 GMT
Sections
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth

Future historians will find that the material characteristic of the current era is...

Aluminium
Plastic
Oil
Water
Carbon dioxide
Copper
Iron
Silicon
Salt
Uranium
Hydrogen
Other

Results (729 votes), past polls