in reply to difference between my and local

my creates a new variable which is only valid inside a sub or block
Yes, to be more accurate: my creates a variable that exists till the end of the innermost enclosing block.
local uses the global var and saves its original value and you can give it a new value which is also only valid inside a sub or block
Yes, and the value exists till the innermost enclosing block is exited.
So..... both create a new variable.
How did you draw that conclusion? In your description of local you say it uses the global variable, and saves its original value, giving you a new value. That's correct - it uses the global variable.

The following is an easy reminder of the difference:

my creates a new variable; local creates a new value.

Note also that a myed variable isn't visible outside the block it's declared in, while a localized value is:

#!/usr/bin/perl use strict; use warnings; use vars '$global'; my $lexical; $global = 'outer'; $lexical = 'outer'; sub show { printf "\$global is '%s'; \$lexical is '%s'\n", $global, $lexical; } show; { local $global = 'inner'; my $lexical = 'inner'; show; } __END__ $global is 'outer'; $lexical is 'outer' $global is 'inner'; $lexical is 'outer'
Perl --((8:>*

Replies are listed 'Best First'.
Re^2: difference between my and local
by jeanluca (Deacon) on Nov 15, 2005 at 13:48 UTC
    Good examples, I've never used stict and warnings, but now those make more sense too.

    Can someone give me an example in which the difference between our and my gets clear ?
    Thanks a lot!!!!!!
    Luca

    ps I should have added our too to the title of this post
      $ perl -Mstrict -lne 'our $c += () = /foo/g; END { print $c }' abc foo foo foo 3 $ perl -Mstrict -lne 'my $c += () = /foo/g; END { print $c }' abc foo foo foo 1

      Ok: always use strict. But not in one-liners...

      So our is somewhat "exotic" in that it declares a package variable, but in a lexical scope.

      Update: removed the statement modifiers that had inadvertently (yes!) slipped in as per Perl Mouse's remark

        If that's the best one can do to explain the difference between our and my, I'd have to say the Anon troll who's been going round lately proclaiming Perl is dead is right.

        Djees. my in combination with statement modifiers are awkward, and in some cases broken. Could you explain why the latter code prints 1 and not 3 (or throw an error?). And why the former prints 3, and not the same result as the latter? (Hint, it has to do with the runtime effects of my).

        So our is somewhat "exotic" in that it declares a package variable, but in a lexical scope.
        Eh, not quite. It declares a package variable - but because it's a package variable, it's not lexically scoped. All that's lexically scoped is the name.
        { # Scope 1 our $s = "foo"; # Sets $main::s } { # Different scope. print our $s; # *Same* variable! } __END__ foo
        our just means: from now on, till the end of the innermost enclosing block, whenever I refer to the variable(s) following our, I mean the package variables with the same name, in the current package (as per use of our, not necessarely per use of the variable).

        So, my gives you a new lexical variable, local gives you a temporary (not lexical!) value, and our gives you a lexical alias or shortcut.

        Perl --((8:>*
Re^2: difference between my and local
by merlyn (Sage) on Nov 15, 2005 at 17:05 UTC
    Yes, to be more accurate: my creates a variable that exists till the end of the innermost enclosing block.
    And to be even more accurate, it creates a variable that has a name that exists until the end of the block (or until the name is shadowed by a duplicated identical name). The variable itself might persist beyond the end of the block (as an anonymous variable), if there are still other references to the variable.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.