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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Don't local(@_)
by blakem (Monsignor) on Jan 13, 2002 at 06:46 UTC | |
|
Re: Don't local(@_)
by hossman (Prior) on Jan 13, 2002 at 06:50 UTC | |
|
Re: Don't local(@_)
by robin (Chaplain) on Jan 13, 2002 at 23:35 UTC | |
|
Re: Don't local(@_)
by dmmiller2k (Chaplain) on Jan 14, 2002 at 20:39 UTC |