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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I thought I knew this, but why do I put my use statements inside if/then if it happens at compile time?

As Tachyon points out, you shouldn't. Or at least, run-time statements won't get executed at compile time, so your code won't do what you want.

Remember, the use() command has an implicit BEGIN block around it, which forces the statements to be executed at compile time. The statement:

use module;

is like writing:

BEGIN { require ";" module::import(); }

So, you have two choices. You can make all the decisions about which code to include at run-time, and use "require" statements.

As ysth points out, something like the following doesn't actually work, though I initially thought it did.

use warnings; use strict; my $config; BEGIN { if ($^O eq 'MSWin32') { use Win32::TieRegistry( Delimiter=>"/", ArrayValues=>0 ); # load $config from registry } else { # else load $config from flat files } } # end system specific compile time code

Since BEGIN blocks and use statements both now happen at "compile time", the above code should work, right? Nope. There's a specifically defined order to how BEGIN blocks execute, and it's not what I had assumed it was.

Upon a careful reading of the perl module documentation page, (type "perldoc perlmod" to read it), I think I understand how it all works. Comments and corrections are appreciated.

According to the documentation: A "BEGIN" code block is executed as soon as possible, that is, the moment it is completely defined.

The documentation doesn't say exactly what "completely defined" means, but I took it to mean when the end of the BEGIN block is reached, when reading sequentially through the code. This implies that code like this:

BEGIN { print "-one-"; BEGIN { print "-two-"; BEGIN { print "-three-; } # end of block that prints "-three-" } # end of block that prints "-two-" } # end of block that prints "-one-"
should print "-three-two-one", because the end of the block that prints "-three-" is encountered first, then "-two-", then "-one-". This is what happens when I run it, which implies I'm at least close to right.

It also means that when you write a BEGIN block with a "use" statement inside it, the end of the use statement gets reached before the end of the BEGIN block. Since a use statment is really an implied BEGIN block, this means it executes first, before the BEGIN block gets a chance to do anything.

Sorry for any confusion I caused. :-( --
Ytreq Q. Uiop

In reply to Re^3: use and require inside subs by Ytrew
in thread use and require inside subs by Anonymous Monk

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, 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
  • 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (4)
    As of 2020-08-12 22:57 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (68 votes). Check out past polls.