Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
our is a lexically scoped alias for a global variable. Will create the global if it didn't exist already.

See the following code:
#!/usr/bin/perl our $a = "foo"; print "$a\n"; package Our; print "$Our::a <--shouldn't print anything here\n"; print "$main::a <--should print 'a' from the main symbol table here\n" +; print "$a <--should also print 'a' from the main symbol table he +re\n\t\tdue to lexically scoped alias\n";
Results are:
foo <--shouldn't print anything here foo <--should print 'a' from the main symbol table here foo <--should also print 'a' from the main symbol table here due to lexically scoped alias
By default any global variables you create automatically populate the "main" symbol table, %main::. With a package declaration you could change which symbol table you are populating when you declare unqualified variables.

But our is a lexical decalaration. In the case above the declaration was file scoped. That's why I still got something back when I gave it an unqualified print statement print "$a\n" after the package declaration.

Don't forget just because it's a lexically scoped variable, it's still an alias for a global variable. Hence when I reassigned $a = "foo" with the our declaration, I blew away the old value.

Any comments are welcome.

In reply to Re: Perl keywords for declaring variables: my, our and local??? by krusty
in thread Perl keywords for declaring variables: my, our and local??? by jira0004

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (6)
    As of 2018-03-19 18:48 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (246 votes). Check out past polls.