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

my and local variable significance in a particular case

by ghosh123 (Monk)
on Apr 24, 2012 at 03:27 UTC ( #966719=perlquestion: print w/ replies, xml ) Need Help??
ghosh123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monk, Suppose I have perl script which has no 'use strict'/'use warnings' in it. In that case what is the significance of a 'my' and 'local' variable in it. It is like

#!/usr/bin/perl my $a = 5; local $b = 4;

What are the significance of 'my' and 'local' here. For your information I know about local variables. They work on global (i.e package ) variables, applying 'local' on them localizes their value and restores back their original value once the scope is gone. But in this above scenario how do they(my and local) differ ?

Comment on my and local variable significance in a particular case
Download Code
Re: my and local variable significance in a particular case
by NetWallah (Abbot) on Apr 24, 2012 at 05:34 UTC
    The code you have posted is not a "scenario" - it merely declares variables.

    Without a usage context, the "my" and "local" have no significance whatsoever.

    If you explain how you would like to use them, we will be happy to help you choose between alternatives, if the fine manual does not make that clear.

                 All great truths begin as blasphemies.
                       ― George Bernard Shaw, writer, Nobel laureate (1856-1950)

Re: my and local variable significance in a particular case
by GrandFather (Cardinal) on Apr 24, 2012 at 05:38 UTC

    Lexical variables are scoped to the current block (which may be the entire file and in the sample case) and must be declared using my. local, as you say, tucks away the previous value of something (it need not be a package variable btw) and restores it when the current scope is exited.

    I suspect you pretty much already knew that. Maybe the missing piece is that you should generally use my (lexical variables) unless you need the extra behaviour that local provides. local doesn't give the scoping protection that a lexical variable does and it incurs some runtime overhead. Bottom line: if you want a variable that is scoped to the current block use a lexical variable.

    True laziness is hard work
Re: my and local variable significance in a particular case
by lidden (Deacon) on Apr 24, 2012 at 05:40 UTC
    Variables declared with my are lexical variables and variables with local are dynamicaly scoped.
Re: my and local variable significance in a particular case
by JavaFan (Canon) on Apr 24, 2012 at 07:33 UTC
    Suppose I have perl script which has no 'use strict'/'use warnings' in it. In that case what is the significance of a 'my' and 'local' variable in it.
    You seem to be under the impression that "strict" and "my" are related. They are not, although if you read Perlmonks you may get the impression they are.

    my creates a lexical variable. It's the only way to create a lexical variable, and the variable is always lexical. Whether or not strict or warnings are enabled.

    local creates a temporary new value for a package variable (technically, the implementation is slightly different, but for most practical purposes, you won't notice) -- the old value is restored when leaving the current, lexical, scope. However, this temporary value is visible from everywhere, even if you call something in a different scope. The old, stashed away, value isn't accessable from Perlland.

    And again, whether or not strict or warnings are enabled is utterly irrelevant.

    As for your program, add the following, and see the difference:

    print $::a, "\n"; # prints the empty string print $::b, "\n"; # prints 4

      Hello JavaFan, Thanks a lot for your reply. I just have one question , does $::a mean $main::a and similarly $::b equivalent to $main::b ? Thanks.

        Yes.
Re: my and local variable significance in a particular case
by tobyink (Abbot) on Apr 24, 2012 at 09:31 UTC
    $x = "x"; $y = "y"; sub uc_call { my $x = uc $x; local $y = uc $y; shift->(); } my $code = sub { print "$x $y\n" }; uc_call($code);

    The above prints lower-case "x" but upper-case "Y", which illustrates that even without strictures, there's a difference between local and my.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: my and local variable significance in a particular case
by brx (Pilgrim) on Apr 24, 2012 at 15:59 UTC
    Note that $a and $b are special variables !

    See perlvar: $a $b : Special package variables when using sort(), see sort. Because of this specialness $a and $b don't need to be declared (using use vars , or our()) even when using the strict 'vars' pragma. Don't lexicalize them with my $a or my $b if you want to be able to use them in the sort() comparison block or function.

    Read documentation about local too.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-08-02 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (54 votes), past polls