Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: An eighth use of local

by itub (Priest)
on Dec 01, 2005 at 16:06 UTC ( [id://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.

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'

      -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.

      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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-03-28 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found