Imo, the lexical / package distinction is not always helpful
to beginners. Here's, er, my shot at explaining this...


1. { }.

The { } pairs that appear around blocks of code represent
a 'lexical scope'. This scope decides the life time of any
name declared with my/our/local.

A file and an eval have an imaginary { } pair around them.


2. My.

My introduces a new name that applies within its enclosing
lexical scope, ie the immediately enclosing { } pair.

If you don't introduce the name with a value, it starts out with
an undef value.


3. Our.

An our name is just like a my name. The first key difference
is that the name ALSO refers to an existing name imported
from outside your code, typically via a 'use' statement such
as:

    use Something;

The second key difference is that, if you don't introduce the
name with a value, it retains whatever value it already had
as established by whatever it was imported from.


4. Local.

A local variable is just like an our variable. The first key
difference is that it ALSO saves the current value of the
existing imported variable, and when the lexical scope
(the { } pair) is exited, the old value is restored.

The second key difference is that if you don't introduce
the name with a value, it starts out with an undef value.

You seldom need to use local.


hth.

Reply via email to