Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: An eighth use of local

by Perl Mouse (Chaplain)
on Dec 01, 2005 at 16:20 UTC ( #513322=note: print w/replies, xml ) Need Help??


in reply to Re: An eighth use of local
in thread An eighth use of local

No, it's not a special case of #1. It's a special case of something that's unmentioned: creating temporary values in aggregates. Consider this:
#!/usr/bin/perl use strict; use warnings; my @things = qw /foo bar baz/; sub do_print { print "@things\n"; } sub show { local $things[1] = 'qux'; do_print; } show; do_print; __END__ foo qux baz foo bar baz
In the subroutine show, local is used to temporary set the value of the second element to something else. Upon leaving the block, the old value is restored.

This is not mentioned in Dominus' article, and this is what rinceWind is using. The fact that %ENV is special is irrelevant.

Perl --((8:>*

Replies are listed 'Best First'.
Re^3: An eighth use of local
by xdg (Monsignor) on Dec 01, 2005 at 16:51 UTC
    It's a special case of something that's unmentioned: creating temporary values in aggregates.

    That's a great point. If you missed it, consider this:

    my $item = 'wibble'; my @things = qw /foo bar baz/; local $things[1] = 'qux'; # Legal local $item = 'wobble'; # Dies with 'Can't localize lexical variable + $item'

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re^3: An eighth use of local
by ikegami (Pope) on May 19, 2006 at 16:33 UTC
    Creating temporary values in aggregates is mentioned under #6 (Dynamic Scope Revisited)
Re^3: An eighth use of local
by itub (Priest) on Dec 02, 2005 at 02:04 UTC
    Good point. I'd still say that it has a bit of #1 due to the magic of %ENV, but it is true that it is also creating a temporary value for a hash element. The @ARGV example, however, was using "normal" localization and affected the whole array.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://513322]
help
Chatterbox?
[marto]: I'm sure it's only going to be a matter of hours before someone adds that comment
[marto]: or closes the question because it's a almost the same as one someone else asked, years ago...
[marto]: snackoverflow gamification of editorial powers for the win :/
marto should register snarkoverflow
[marto]: nothing to see here people, go about your daily business :P

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2018-01-23 10:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (243 votes). Check out past polls.

    Notices?