If you run your code through perl -MO=Xref mytest.pl, you'll get the following output...

----- unneeded portion of output not shown ----- Package aaa &do_a s14 Package bbb &do_b s24 Package ccc &do_c s25 Subroutine (main) Package (lexical) $var i10, i21, i34, 27 Package aaa &do_a &23 Package bbb &do_b &24 Package ccc &do_c &25 Subroutine aaa::do_a Package (lexical) $var 13 Subroutine bbb::do_b Package (lexical) $var 37 Subroutine ccc::do_c Package (lexical) $var 48 mytest.pl syntax OK

The important part of all that is:

Package (lexical) $var i10, i21, i34, 27

That's saying that $var is being declared in line 10, 21, and 34. ...and used in 27. This confirms what others have previously mentioned in this thread; that packages don't create their own lexical scopes. Files create lexical scope, and curly brackets do. So it's common to think of a package as its own lexical scope, since it's also common (though not a requirement) for packages to live alone in a file. But you can't rely on that logic because multiple packages in the same file reside in the same scope.

Update: On a side note, I do think that B::Xref's output is a little misleading by saying, "Package (lexical) $var .....". It seems that it ought to say something more akin to "File (lexical) $var" or "Block (lexical) $var ..."

I'm kind of interested in hearing the reason for why it does what it does.


Dave


In reply to Re: Scope, package, and 'my' variables by davido
in thread Scope, package, and 'my' variables by ff

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.