Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: An eighth use of local

by itub (Priest)
on Dec 01, 2005 at 16:06 UTC ( #513315=note: print w/ replies, xml ) Need Help??

in reply to An eighth use of local

%ENV and @ARGV are special variables, so it is indeed a special case of use #1. However, it is good to point it out as a practical example.

Comment on Re: An eighth use of local
Replies are listed 'Best First'.
Re^2: An eighth use of local
by Perl Mouse (Chaplain) on Dec 01, 2005 at 16:20 UTC
    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:>*
      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'


      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.

      Creating temporary values in aggregates is mentioned under #6 (Dynamic Scope Revisited)
      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?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2015-11-25 20:35 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (690 votes), past polls