Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

my and local??

by mrt (Acolyte)
on Jul 28, 2000 at 13:15 UTC ( #24814=perlquestion: print w/replies, xml ) Need Help??

mrt has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, Can u pls explain me what is the actual difference between declaring variables with my and local... I have read that it is my which should be used generally as it is faster given a choice between two?? but does local runs fast in some situations it becomes too complicated for me..after this...pls help..:-mrt

Replies are listed 'Best First'.
RE: my and local??
by davorg (Chancellor) on Jul 28, 2000 at 13:40 UTC
more fun with my and local??
by jlistf (Monk) on Jul 28, 2000 at 17:46 UTC
    some repeat of previous posts, but:

    my - creates a lexical variable. this variable is a temporary variable, only visible within the block in which it was declared. also provides deep-binding which is used to create closures.

    local - creates a dynamic variable. this variable replaces the value of a package global variable until the end of the block. sub-blocks and subroutines called from this block retain this local value. does not provide deep-binding and so cannot be used to create closures.
Re: my and local??
by monk (Scribe) on Jul 28, 2000 at 16:32 UTC
    The basic difference is that, when you declare $foo = 3; and then
    you go into a block or sub and say local $foo = 5; the value in $foo
    inside the blocks or sub is 5, but outside is 3 here is some code:
    #!/usr/bin/perl -w #didn't use strict, to make the example clearer. $foo = 3; #default value of $foo { local $foo = 5; #now the value of $foo is 5 while inside the braces print "$foo\n"; # prints 5. } print "$foo\n"; # prints 3.

    With my is a bit different code examples:
    #!/usr/bin/perl -w use strict; #<- very important. my $foo = 3; # $foo has a value of 3, and is seen by the compiler. { my $bar = 5; # $ bar life is limited to the block. print "$bar\n"; # you get 5. print "$foo\n"; # you get 3. } print "$bar\n"; # <- compiler gives error.$bar does not exsist (kind o +f). print "$foo\n"; # you get 3.
    I hope this clear, things up a bit, and i hope i'm not completely wrong
    if i am i'm sure the other monks, will point it out.
    monk

    Update:
    s/compiler/interpreter/ig;


    monk
Re: my and local??
by merlyn (Sage) on Jul 28, 2000 at 16:36 UTC
    The simplest explanation is something like:
    • my creates a temporary variable that is not in any package
    • local creates a temporary value for a global (package) variable

    -- Randal L. Schwartz, Perl hacker

Re: my and local??
by toadi (Chaplain) on Jul 28, 2000 at 13:27 UTC
    </CODE> WARNING: In general, you should be using `my' instead of `local', because it's faster and safer. Exceptions to this include the global punctuation variables, filehandles and formats, and direct manipulation of the Perl symbol table itself. Format variables often use `local' though, as do other variables whose current value must be visible to called subroutines. </CODE>

    This is what is written in man perlsub.

    I think this is kind of straightforward?

    --
    My opinions may have changed,
    but not the fact that I am right

RE: my and local??
by Adam (Vicar) on Jul 28, 2000 at 22:10 UTC
    Read merlyn's and jlistf's posts above. The only thing I wanted to add was to make it clear that local restores the old value after the localized variable goes out of scope. This has both good and bad repercutions. Good in that it allows for things like a recursive function that uses a global variable, bad in that the variable could be created inside a block, and when the block goes out of scope you are holding undef.

    Also, the best text explanation I've seen on all this is Sriam Srinivasan's "Advanced Perl Programming" available from O'Reilly. I think that's a black panther on the cover. (My HS mascot)

      A good place to read on uses of local is MJDs Seven Useful Uses of local article. Sort of an aside, but a good article.

      Cheers,
      KM

        Welcome Back KM!!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://24814]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2020-11-27 21:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?