Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

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

The package { ... } part is actually irrelevant. You can reproduce this behavior with a much simpler script, which might illustrate the issue more clearly:

say_foo(); my $foo = 'foo'; say_foo(); sub say_foo { say '$foo = ' . ($foo // '(undef)') } __END__ $foo = (undef) $foo = foo

At the first say_foo() call, the line my $foo = 'foo' has not been executed yet. Simple enough, but now you may be wondering why you're able to call say_foo() at all at that point in the script. The short answer is, forward definitions (and declarations) are a language feature. Otherwise it would be more difficult to have circular subs (a() calls b() and b() calls a(), possibly with even more complex indirection).

To really drive the point home, it might be helpful to look at how BEGIN { ... } works. BEGIN will run the code in its enclosed BLOCK as soon as it is completely defined, so you can do something like this:

say_foo(); my $foo; BEGIN { $foo = 'foo' } say_foo(); sub say_foo { say '$foo = ' . ($foo // '(undef)') } __END__ $foo = foo $foo = foo

Consider this to be illustrative, not necessarily prescriptive. BEGIN is great, but it's not always the best tool for the job. It depends on the situation (and so I'd be happy to dive a little deeper if you want to go into more detail on what you're trying to do). I typically keep my globals at the top (and of course only using globals when necessary), and separating packages out into their own source files, and so I hardly ever (never?) need BEGIN for this sort of thing.

use strict; use warnings; omitted for brevity.

In reply to Re: why package definition order affect the available of package variable by rjt
in thread why package definition order affect the available of package variable by fanasy

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 contemplating the Monastery: (2)
As of 2024-04-19 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found