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

Re: Whether to use local()

by shotgunefx (Parson)
on Mar 15, 2002 at 18:31 UTC ( [id://152042]=note: print w/replies, xml ) Need Help??


in reply to Whether to use local()

local only works on global variables. (Anything in the symbol table.) my (lexicals) variables don't exist in the symbol table.

update
I realized I didn't really answer the question you where trying to ask. Declaring a my variable at the top of a file at worst is polluting one file. If you are writing the code and the files aren't huge, it's not all that hard to avoid shooting yourself in the foot with a little dilligence.

-Lee

"To be civilized is to deny one's nature."

Replies are listed 'Best First'.
Re (tilly) 2: Whether to use local()
by tilly (Archbishop) on Mar 16, 2002 at 01:15 UTC
    local only works on global variables

    my %foo; $foo{bar} = " odd then?\n"; { local $foo{bar} ="Isn't this"; print $foo{bar}; } print $foo{bar};
    :-P
      I know about local on hash elements and array elements but thought it would complicate the explanation for him. Just like I omitted PadWalker from the explanation of accessing lexicals. Point taken though. Was local on hash/array elements added as a hack for signal handlers? It seems the only real use that I've seen. Or is it used internally when you localize a package var in the symbol table seeing a package is in a sense a hash?

      -Lee

      "To be civilized is to deny one's nature."
        Was local on hash/array elements added as a hack for signal handlers?

        local was added to perl when there were only global variables as a hack for providing a means of creating "local" variables. There are other uses for it though, but that is better said here: Seven Useful Uses of local

Re: Re: Whether to use local()
by jerrygarciuh (Curate) on Mar 15, 2002 at 18:38 UTC
    So you can't use local on vars you create if you are going to use strict? That seems strange to me.
    TIA
    jg
    _____________________________________________________
    It's not my tree.
      You can use both globals (package vars) and lexicals under use strict though globals need to be fully qualified ( $My::Mail::Server= "255.255.255.255";)
      Though globals are usually (not always) the wrong way to go. The idea of lexicals are that they only exist in a scope thereby limiting access. What local does is let you temporarily save a value for a package var. It's a runtime trick and my is (mostly) a compile time trick.

      The lowdown is package vars exist in packages and any can be accessed from any package. my vars exist in the scope they where declared in and cannot be accessed outside so you don't have to worry about other subs/modules etc clobbering your values. You usually use local to save the value of $/ or $_ so you don't screw up code that calls your code. It has some other uses but that's one of the main one. There are probably a lot better explainations than mine and I'm sure if you do a search for lexical / package / global you will find a wealth of info here in the monestary.

      UPDATE
      here is a great FAQ on "Coping with Scoping" by dominus. It's a far better explaination than mine.

      -Lee

      "To be civilized is to deny one's nature."

        local (the keyword) has NOTHING to do with local variables. In Perl, you use my (the keyword) to declare local (i.e., lexically scoped, or 'lexical') variables.

        The keyword local localizes a variable, whether it's named or anonymous or global (i.e., package variables, which are in the symbol table). To localize a variable is to stash away its value in such a way that exiting the enclosing scope will result in its being restored. For example:

        use vars qw( $x ); # a package global $x = 'outer'; # this block: { my $save_x = $x; # localize $x by saving its value $x = 'inner'; # change $x to hearts content some_sub( $x ); # some_sub() sees $x with value 'inner' $x = $save_x; # restore original value just before exiting b +lock } # is functionally identical to this block: { local $x = 'inner'; some_sub( $x ); } # in both cases, some_sub() is called with the package global $x set # to 'inner' and in both cases, the value of $x is restored when the # block exits, except in the 'local' case, it is not necessary to # explicitly restore $x

        One example of its use is with the Perl global, $/. Often, (in a subroutine, for example) it is necessary to set $/ = undef;, but to do so may break code which assumes its default value.

        # need to 'slurp' in an entire file { local $/ = undef; # (read the file, etc.) } # $/ is now restored to whatever it was before

        Hopefully, this explains WHEN to use local.

        Update: Whoops! I stand corrected (thanks, shotgunefx)! Starting with Perl 5.5, it became possible to localize arbitrary anonymous variables (e.g., local $obj->get_handle()->{some_property}->get_param("param1");). I never actually had a use for (and therefore never tried) localize a lexical; I suppose I just assumed that they could.

        Apologies to all for suggesting that localizing a lexical was possible.

        dmm

        If you GIVE a man a fish you feed him for a day
        But,
        TEACH him to fish and you feed him for a lifetime

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-24 12:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found