in reply to Re: Controlling "use" statements
in thread Controlling "use" statements

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?

Replies are listed 'Best First'.
Re^3: Controlling "use" statements
by shmem (Chancellor) on Jun 13, 2007 at 19:37 UTC
    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}
Re^3: Controlling "use" statements
by ikegami (Patriarch) on Jun 13, 2007 at 19:47 UTC

    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.