in reply to BEGIN and 'use' question

A BEGIN block will execute immediately (I use this term loosely:-), before any other code is run. This means your begin block runs before the comparison is made by the'&&' operator.

To fix this, you want to check the value of DEBUG inside the BEGIN block, not prior to it.

Update:Thanks, Elian for pointing out my mistake. I was answering why the begin block was executing regardless of the value of DEBUG. I didn't look carefully inside the BEGIN block to see that the OP was trying to 'use' a module in there.

Replies are listed 'Best First'.
Re: Re: BEGIN and 'use' question
by legLess (Hermit) on May 07, 2002 at 20:47 UTC
    Aha! That's the information I needed - thanks!
    --
    man with no legs, inc.
      It's wrong, though.

      use is a compile time statement. As soon as the compilation phase gets to the end of the statement, the use is immediately executed. No conditionals will affect it (well, there's some stuff added recently, but we'll skip that for the moment) and it will happen. Throw a use inside a BEGIN block and all that'll happen is that the use will get used before the BEGIN block is fully parsed.

      If what you want is conditional inclusion, you need to do it the longer way, with require and import, both of which are runtime statements. Something like:

      BEGIN { if (DEBUG) { require foo; import foo; } }
      There's more documentation on this in the use section of perlfunc.