Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Ask a Silly Question...

by Util (Priest)
on Feb 01, 2008 at 03:05 UTC ( #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?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2018-09-23 08:02 GMT
Find Nodes?
    Voting Booth?
    Eventually, "covfefe" will come to mean:

    Results (190 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!