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

Could you please clear my doubt about the difference between 'my' and 'local' variable with some sample code

thank you

Replies are listed 'Best First'.
Re: Difference my and local
by Roy Johnson (Monsignor) on May 25, 2005 at 02:03 UTC
    The difference is that a local replaces a global in the current block and in any subroutines that are called from the current block. my is only in effect for the enclosing block (or package thanks revdiablo or file).
    use strict; use warnings; our ($x, $y) = ('global X', 'global Y'); example(); sub example { local $x; my $y; $x = 'changed local'; $y = 'changed my'; example2(); } sub example2 { print "X: $x, Y: $y\n"; }
    example2 will print the $x that is local to example, but the global $y.

    Caution: Contents may have been coded under pressure.

      Well, as a rule of thumb: use my everywhere, and only if perl complains about, think about using local (e.g. with build in perl variables)

      With local, you can also localize parts of datastructures, e.g.

      use warnings; use strict; use vars qw(@array); @array = 1..20; print "BEFORE: @array\n"; &TestSub(); print "AFTER: @array\n"; sub TestSub { local @array[4..10]; @array[4..11] = 104..111; print "INSIDE: @array\n"; }
      which outputs something like:
      BEFORE: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 INSIDE: 1 2 3 4 104 105 106 107 108 109 110 111 13 14 15 16 17 18 19 2 +0 AFTER: 1 2 3 4 5 6 7 8 9 10 11 111 13 14 15 16 17 18 19 20

      beware that 111 is not "localized" and so the change is global.

      Best regards,
      perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: Difference my and local
by mifflin (Curate) on May 25, 2005 at 03:34 UTC
    Here is a good article on Scoping that Mark Jason Dominus wrote for The Perl Journal.
    http://perl.plover.com/FAQs/Namespaces.html
    Although the whole article is good, skip on down to the section titled "Local and My"
Re: Difference my and local
by gopalr (Priest) on May 25, 2005 at 02:05 UTC

    Hello

    my:
    your subroutine needs temporary variables. You shouldn't use global variables, because another subroutine might also use the same variables.

    local:
    All subroutines called from the context of a local variable can access that variable.

    $a='one'; $b='two'; sub1(); sub sub1 { local $a = 'three'; my $b = 'four'; sub2(); } sub sub2 { print "\$a: $a\n"; print "\$b: $b\n"; }

    Output

    $a: three $b: two
Re: Difference my and local
by ikegami (Patriarch) on May 25, 2005 at 02:52 UTC

    my variables are statically bound.

    sub test_a { my $var = "Hello World\n"; } sub test_b { print($var); # XXX Doesn't print Hello World. } test_a(); print($var); # Doesn't print Hello World.

    local variables are dynamically bound.

    sub test_a { local $var = "Hello World\n"; } sub test_b { print($var); # Prints Hello World. } test_a(); print($var); # Doesn't print Hello World.

    However, dynamic binding is rarely used, because its use leads to fragile software. In Perl, local is mostly used to assign a new value to a builtin global, while automatically restoring the global's value when the block is exited.

    sub test { $_ = $_[0]; s/([a-z])/uc($1)/eg; return $_; } foreach (qw(a b c)) { print(test($_), "\n"); } $ perl script.pl Modification of a read-only value attempted at script.pl line 3.

    It's trying to modify the constant 'a', since $_ is a reference to it. Fix:

    sub test { local $_ = $_[0]; s/([a-z])/uc($1)/eg; return $_; } foreach (qw(a b c)) { print(test($_), "\n"); } $ perl script.pl A B C
Re: Difference my and local
by merlyn (Sage) on May 25, 2005 at 12:06 UTC
Re: Difference my and local
by chas (Priest) on May 25, 2005 at 02:57 UTC
    See also my reply in 444526.
    chas
Re: Difference my and local
by cosimo (Hermit) on May 25, 2005 at 16:33 UTC