in reply to when is "my" not necessary?

In addition to what runig said, note that Perl has two places to store variables. The first is the lexical scope pad. That's where my variables go. Lexical scopes can be created and destroyed by a lot of things, but the most common is within blocks:

my $n = 1; { my $n = 2; print $n; # Prints 2 } print $n; # Prints 1

The second place is the symbol table. This is where variables declared our and local go (among other things). The symbol table can be accessed directly from Perl code (you need a C-based backend to play with the lexical pad directly).

Using lexicals is usually the Right Thing.

"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Replies are listed 'Best First'.
Re^2: when is "my" not necessary?
by Mutant (Priest) on Nov 02, 2004 at 20:57 UTC

    Lexical scopes can be created and destroyed by a lot of things

    Out of interest, what else can destroy them?

      A package statement. The end of a string eval. Probably a bunch of other stuff I can't remember ATM.

      "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

        In addition to blocks and string evals, files required or used create lexical scopes.