Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

So I don't know if this belongs in SoPW or Meditations, but I stumbled across the following (what I would call) inconsistency, and wondered if anybody had an explanation...

This does not work:
use strict 'vars'; BEGIN { *main::foo = \@main::foo } print "Foo: (@foo)\n"; __OUTPUT__ Variable "@foo" is not imported at foo.pl line 10. Global symbol "@foo" requires explicit package name at foo.pl line +10. foo.pl had compilation errors.
But this works fine:
use strict 'vars'; package somePkg; BEGIN { *main::foo = \@main::foo } package main; print "Foo: (@foo)\n";
Somewhat interestingly, this seems to work okay, too:
use strict 'vars'; # We're already in package main BEGIN { *pkg::foo = \@pkg::foo } package pkg; print "Foo: (@foo)\n";
And this fails, as well:
use strict 'vars'; package pkg; BEGIN { *pkg::foo = \@pkg::foo } print "Foo: (@foo)\n";
I noticed that running "perl -MO=Deparse" on the last snippet, above, shows the symbol table manipulation being (*cough*) "optimized" (*cough*) thusly:
package pkg; sub BEGIN { *foo = \@foo; }

Perhaps that's why "strict" doesn't allow me to access the unqualified "@foo" directly? Because -- since it's not in a different package -- something, somewhere, isn't creating the corresponding lexical alias? *Shrug*

Finally, this is mostly the same as number 2, above, which also works, but in the fewest lines:

use strict 'vars'; BEGIN { package pkg; *main::foo = \@main::foo } print "Foo: (@foo)\n"; __OUTPUT__ Foo: ()

PS - Before you say "Just use 'our'" ...

Yes, I know. There's More Than One Way To Do It, and I'm MORE curious about the underlying question of why THIS way happens to fail.

Thanks! :-D


In reply to Creating "Lexical" Symbol Table Aliases (a la "use vars") From The Same Package by OneTrueDabe

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (5)
    As of 2014-11-21 04:29 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (105 votes), past polls