Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-20 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found