Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Ask a Silly Question...

by Util (Priest)
on Feb 01, 2008 at 03:05 UTC ( [id://665506]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://665506]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-28 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found