Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

An eighth use of local

by rinceWind (Monsignor)
on Dec 01, 2005 at 09:49 UTC ( #513294=perlmeditation: print w/replies, xml ) Need Help??

Musing on Mark Jason Dominus's Seven Useful Uses of local, I think I've come up with another one.

The most common use I have for local is with %ENV. The article doesn't specifically mention environment variables. There are instances where I am doing one of the following:

  • Forking several times with %ENV{FOO} set to different values, and I need to keep its original or unset value in the parent.

  • Writing tests for something that reads an environment variable (see Module::Optional for an example)

  • Opening Tk top level windows on multiple displays (see Re: Tk - is MainWindow robust?)

Thinking about it more, I would suggest that @ARGV belongs in the same category, as used in the file slurp idiom:

my $slurp = do { local (@ARGV, $/) = 'foo.txt'; <>};

There's also localising @ARGV for handling INCLUDE directives in the input stream.

These uses of local could be thought of as a special case of number 6 (Dynamic Scope Revisited), or possibly number 1 (Special Variables), but I think they are a new category.

Update:Looks like there might be a problem with - try this link as an alernative for MJD's article.


Oh Lord, won’t you burn me a Knoppix CD ?
My friends all rate Windows, I must disagree.
Your powers of persuasion will set them all free,
So oh Lord, won’t you burn me a Knoppix CD ?
(Missquoting Janis Joplin)

Replies are listed 'Best First'.
Re: An eighth use of local
by Ovid (Cardinal) on Dec 01, 2005 at 14:27 UTC

    My most common use of local deals with recursive method calls:

    sub foo { my $self = shift; local $self->{some_val} = $self->get_val; # do a bunch of stuff $self->foo; }

    This seems strange, but I do find this useful at times. It allows me to temporarily overwrite a value which needs to be preserved for a given stack frame.


    New address of my CGI Course.

Re: An eighth use of local
by Perl Mouse (Chaplain) on Dec 01, 2005 at 10:00 UTC
    I think that temporary values for elements of aggregates was one of the first 'eigth use's to arise after Mark wrote his article. Probably the most common usage of this is the temporary signal handler.

    Note that his article is quite old. For over five years, there has been no need for localized file- or dirhandles. Uses of localized file- and dirhandles could be moved to the last point: relics of the past.

    Perl --((8:>*
Re: An eighth use of local
by itub (Priest) on Dec 01, 2005 at 11:06 UTC
    %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.
      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.
Re: An eighth use of local
by jkeenan1 (Deacon) on Dec 02, 2005 at 08:00 UTC
    Musing on Mark Jason Dominus's Seven Useful Uses of local, I think I've come up with another one.

    I got a "404 link not found error" when I clicked on your link. Could you double check? Thanks.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://513294]
Approved by Limbic~Region
Front-paged by Courage
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2020-06-07 02:52 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (42 votes). Check out past polls.