Globals live in the package global symbol table. Lexical variables do not reside in the package global symbol table. They are held in a sort of internal pad. my variables (actually known as lexical variables) have their own private namespace, so to speak.

our is a kind of new-fangled way to declare package globals within a particular scope. It really has little effect unless you're using strict. But they're definately not the same thing as lexicals. You can't have two 'our $this' in the same package namespace and expect them to be different variables. They're the same variable.

Consider the following code:

{ our $global = 10; # Declare a package global. my $lexical = 10; # Declare a block-scope lexical. { our $global = 20; # Redeclare same package global. my $lexical = 20; # Declare a new lexical in narrower scope +. } # Narrower scope has ended. Inner $lexical disappears. # $global remains. print "Global: $global\nLexical: $lexical\n"; } __OUTPUT__ Global: 20 Lexical: 10

Since there is only one symbol table per package, that package global declared with our is the same one throughout this script. But there can be as many different lexical variables of the same name as there are scopes to fit them in; each one is in its own namespace, which we (without XS) don't really have access to.

By the way, the variable declared by our $global is the same variable as $main::global, and $::global, and ${$::{global}} (assuming we're in package main). Each syntax has its own uses, but we don't usually see some of them in everyday code.


Dave


In reply to Re: Dumping symbol table shows variables declared with our but not my by davido
in thread Dumping symbol table shows variables declared with our but not my by Popcorn Dave

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.