in reply to Controlling "use" statements

People are suggesting solutions. Let me identify the problem:
BEGIN { # These will only be used in the # debugging version. if ($DEBUG) { use strict; use warnings; use Data::Dumper; use CGI::Carp; } # These are needed by the program # regardless. use CGI::Minimal; use HTML::Template; # ... others, etc. }

Like BEGIN blocks, use statements execute as soon as they are compiled. Your use CGI::Carp; executes as soon as it is compiled, with no regard to whether that code is reached at run-time. That means the if ($DEBUG) has no bearing on whether use statements inside the if are executed or not.

Secondly, strict and warnings are pragmas. They only affect the lexical block in which they are located (and nested blocks). use strict; would only work for the body of the if.

Replies are listed 'Best First'.
Re^2: Controlling "use" statements
by nodice (Initiate) on Jun 13, 2007 at 18:48 UTC
    Secondly, strict and warnings are pragmas. They only affect the lexical block in which they are located (and nested blocks). use strict; would only work for the body of the if.

    Does this mean they are "uncontrollable" by definition?

      No, the pragmas are controllable with use and no, e.g.
      use strict; $MyClass::foo = 'boink'; sub bar { no strict; # strict turned off for this sub } ... # strict again in effect

      They live in files which are imported, like modules - but they act like switches and turn on and off certain flags at compile time, for their lexical scope.

      There's no reason to turn strict or warnings off for a "working" version, once the "development" versions behave with them. Again: *no* *reason*. And in case you ask again - no, there is no reason, period.

      --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}

      No.

      The following works:

      my $DEBUG; BEGIN { $DEBUG = $ARGV[0]; } use if $DEBUG, 'strict'; # <-- The compiler is here because # Perl executes use statements # as soon as it compiles them. # "strict" will work until the # end of this block (namely # until the end of the file). print($test); # strict error if $DEBUG

      The following works:

      my $DEBUG; BEGIN { $DEBUG = $ARGV[0]; if ($DEBUG) { require strict; import strict; } } # <-- The compiler is here because # Perl executes BEGIN blocks # as soon as it compiles them. # "strict" will work until the # end of this block (namely # until the end of the file). print($test); # strict error if $DEBUG

      The point, however, is that the following doesn't work:

      my $DEBUG; BEGIN { $DEBUG = $ARGV[0]; if ($DEBUG) { eval <<'__EOI__'; use strict; # <-- The compiler instance used # by eval is here because # Perl executes use statements # as soon as it compiles them. # "strict" will work until the # end of this block (namely # until the end of the code # passed to eval). __EOI__ } } print($test); # No strict error

      Actually, I thought the second one wouldn't work either, but it does for the reason I gave in the comments.