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

Hi,
my and local variable are only having the scope b/w the braces that were defined.
Why we are having two keywords to do the same thing?
Regards,
Anbarasu

Replies are listed 'Best First'.
Re: Difference b/w my and local var?
by Fletch (Bishop) on Mar 11, 2009 at 13:14 UTC

    They don't. Go read Coping with Scoping and disabuse yourself of that notion immediately.

    Update: And it's not like the documentation for my and local as well as the section in perlsub which they direct for more information don't also explain they're not the same. Then again I guess reading documentation is passé around here now.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      ...nor yet (re)searching before asking! Why enrich your experience when it appears it isn't needed

      A user level that continues to overstate my experience :-))
Re: Difference b/w my and local var?
by ikegami (Patriarch) on Mar 11, 2009 at 13:20 UTC

    They don't do the same thing at all.

    my creates a new variable that's only visible to the scope in which it's defined.

    local doesn't create a variable. local temporary saves the variable and restores it at the end of the scope. Unlike my, local does not limit the scope of the variable. If the variable being localized is a package variable (and it usually is), it's still globally accessible after being localized.

    Always use my whenever possible.

      For clarification of what "doesn't create a variable" means, the following examples might be interesting/surprising (they were for me when I first realized what was happening).

      This is rather OT to the OP's question and also rather esoteric. If one doesn't use aliases or tied variables or directly manipulate symbol tables (stashes) then the distinction/clarification may not be helpful/relevant.

Re: Difference b/w my and local var?
by toolic (Bishop) on Mar 11, 2009 at 13:34 UTC
Re: Difference b/w my and local var?
by vinoth.ree (Monsignor) on Mar 11, 2009 at 13:32 UTC

    Both of them are used to declare local variables.

    The variables declared with "my" can live only within the block it was defined and cannot get its visibility inherited functions called within that block, but one defined with "local" can live within the block and have its visibility in the functions called within that block.

      Both of them are used to declare local variables

      No. local does not declare a variable, as you can easily try out:

      $ perl -Mstrict -e 'local $x' Global symbol "$x" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.

      If local declared a variable you wouldn't get an error message here.

        Yikes!! More fun with definitions...

        What does "declare" mean and what does "local" mean?

        strict says of "strict vars":

        This generates a compile-time error if you access a variable that wasn’t declared via "our" or "use vars", localized via "my()", or wasn’t fully qualified.

        If "localized via "my()"" is reasonable terminology, then surely it is reasonable to say that as a result the variable is "local". And surely "local" creates a "local" variable. So it is no wonder that the gentle reader has concluded that both "my" and "local" produce "local" variables and that the declarative statements "my $var" and "local $var" both "declare" them to be "local".

        It is most unfortunate that there is so much loose use of terminology.

        What your example shows is that, consistent with the documentation of "strict vars", "local $var" does not "declare" the variable in a manner that satisfies the requirements of "strict vars" and that without them being satisfied an unqualified reference to the variable produces an error. The variable is none the less localized (declared to be local???) in the absence of "strict vars" and can be localized with "strict vars" in effect by using its fully qualified name.

        Consider

        #!/usr/bin/perl -w use strict; use warnings; use Devel::Peek; $main::x = 10; { local $main::x = 20; print "$main::x\n"; } print "$main::x\n";

        Which produces

        20 10

        In this example, is it reasonable to say that $x has not been "declared to be local" just because it has not been declared in a manner that satisfies "strict vars" so that it can be referenced by its unqualified name?

      Thank you very much. I got it.
      Regards,
      Anbarasu
Re: Difference b/w my and local var?
by shmem (Chancellor) on Mar 11, 2009 at 22:50 UTC
Re: Difference b/w my and local var?
by boom (Scribe) on Mar 11, 2009 at 13:40 UTC

    The variables declared with my() are visible only within the scope of the block which names them.

    They are not visible outside of this block, not even in routines or blocks that it calls.

    local() variables, on the other hand, are visible to routines that are called from the block where they are declared.

    Neither is visible after the end of the block at all.

      local() variables, on the other hand, are visible to routines that are called from the block where they are declared.

      Not quite. It doesn't make the variables visible outside the block.

      sub foo { ??? how do you access the var ??? } { my %hash; local $hash{foo} = 123; foo(); }

      It doesn't even create variables as your wording suggests. It's not that what you said is wrong, just unclear and/or misleading. (Compare to what I used earlier.)

        Of course if you resort to minor evil there is a way to get at it . . . :)

        use PadWalker qw( peek_my ); sub foo { my $ebil = peek_my( 1 ); print $ebil->{'%hash'}->{foo}, "\n"; } { my %hash; local $hash{foo} = 123; foo(); }

        (Not that that's germane to the correct point you were making; just an interesting aside your ???s brought up :)

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.