in reply to Re^3: repeated use of module and EXPORT
in thread repeated use of module and EXPORT

The basic description is found in perlmod:

A "BEGIN" code block is executed as soon as possible, that is, the moment it is completely defined, even before the rest of the containing file (or string) is parsed. You may have multiple "BEGIN" blocks within a file (or eval'ed string) -- they will execute in order of definition. Because a "BEGIN" code block executes immediately, it can pull in definitions of subroutines and such from other files in time to be visible to the rest of the compile and run time. Once a "BEGIN" has run, it is immediately undefined and any code it used is returned to Perl's memory pool.

Also note that use has an implicit BEGIN block around it.

Replies are listed 'Best First'.
Re^5: repeated use of module and EXPORT
by rpelak (Sexton) on May 23, 2008 at 19:47 UTC
    Yeah I saw that text on the perlmod. But I didn't know that use was implicitly a BEGIN... so that makes more sense... But anyone have any links to a more indepth write up of how the compiler works, like how it chooses what to compile first and all that...

      It's all in perlmod. Search for BEGIN, UNITCHECK, CHECK, INIT and END. But you should really read it all through.

      There's more to be found in the perl sources... ;-)

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        I just read the whole perlmod you sent, and I guess I was looking at an abridged version previously... It did have much greater detail...

        One thing that seems odd...
        our (@ISA,@EXPORT,@EXPORT_OK); BEGIN { @ISA = qw(Exporter); @EXPORT = qw(debugOn); @EXPORT_OK = qw(); }
        The begin doesn't work without the our before it... is our also implicitly a BEGIN? otherwise I wouldn't think the our should effect the BEGIN block as it shouldn't even get executed until after the BEGIN block is done.

        and finally it didn't explicitly say some things that I think I can infer from the way things appear to function, so correct me if I am wrong on any of the below.
        compilation starts on the top script file...
        Whenever a use, or a require in a BEGIN block (or is require implicitly a BEGIN as well?), or anything of that sort is encountered, it stops processing the current file and switches to that one, and doesn't return until that one is totally complete (as in not just the BEGIN blocks of the second file). And if it hits a use of a module it is already in the middle of processing, all it will do is call that modules import sub, and not try to compile it's code a second time (thus preventing loops).
        Sound right?
        Randell