in reply to difference between my and local

You will also see more difference when you use strict, as it will complain when you localize a global variable which is not yet initialized:
"Global symbol (...) requires explicit package name ...".

Cheers
jonix

Replies are listed 'Best First'.
Re^2: difference between my and local
by jeanluca (Deacon) on Nov 15, 2005 at 13:07 UTC
    I get: Can't localize lexical variable $var at ./a.pl line 11
    What exactly does our do ?

    Your example shows that you can only use local on global variables...
    So why use my anyway ?

    Luca
      our creates a lexical scoped name for a global variable.

      Your example shows that you can only use local on global variables
      Well, it more on anything that isn't a lexical variable. You can localize array elements for instance, even if the array itself is a lexical variable.
      So why use my anyway
      Because my creates lexically scoped variables. Variables that aren't visible outside of the block that declares them. local creates temporary values, which are only reset if a block gets exited.

      The right question should be "why use local?".

      Perl --((8:>*
      What exactly does our do ?

      See the documentation.

      So why use my anyway ?

      Because scoped variables are easier to handle than global variables.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg