http://www.perlmonks.org?node_id=665506


in reply to Ask a Silly Question...

Perl has a strict separation between "lexical" variables and "package" variables.

Package vars live in the symbol table, can be declared with "our()" or "use vars qw()" (or just spring into existence when first used if "use strict vars" is not in effect), and can be remotely manipulated with things like Exporter or direct package references like "$Data::Dumper::Sortkeys=1". They always *exist* globally (although our() can control where they can be *referenced*). They are generally used only when you *need* some other package to manipulate your data remotely.

Lexical vars are *not* accessible from the symbol table; they live in the "lexical PAD" that exists in every lexical block. They are never truly global; at best (worst?) they can be scoped to their entire declaring file. These are the vars that are always to be used, unless you have a specific need for a package var.

So, in your three examples:

  1. You declared $FOO as lexical using my(), so it will not exist in the symbol table.
  2. You tried to declare a package var by using my(), which is illegal because it is a contradiction of the lexical/package meanings.
  3. You created package var $main::FOO by simply referring to $FOO while inside package main::.
And ++Fletch - Coping with Scoping is a great article for this subject.