in reply to Making strict optional

Here you go,
eval 'use strict'; if ($@) { # If it cannot 'use strict' then it will put the error in $ +@ print "Cannot 'use strict', it appears to be missing\n"; }
Put this in your code, and it will print a message if it cannot load strict for whatever reason. You can take out the message if you want.

Update: Put the {} for the eval command in the wrong place... Just taking them out altogether and removing it from the BEGIN block. At least I learned something today.

Replies are listed 'Best First'.
Re^2: Making strict optional
by ikegami (Patriarch) on May 31, 2006 at 15:08 UTC

    While that trick can be used for detecting if strict exists, it won't turn strict on.

    strict ends here | | BEGIN { v eval 'use strict'; warn("Cannot 'use strict', it appears to be missing\n") if $@; }

    On the other hand, the following works (and doesn't use eval EXPR):

    BEGIN { eval { require strict }; if ($@) { warn("Cannot 'use strict', it appears to be missing\n"); } else { strict->import(); } }
Re^2: Making strict optional
by Fletch (Bishop) on May 31, 2006 at 15:08 UTC

    Seeing how strict is lexically scoped using it inside of an eval inside of a BEGIN block's scope isn't really going to do the rest of one's code much good . . .

      Well you might say that:

      BEGIN { eval { require strict; }; strict->import() unless $@; } + $foo = 1;
      Gives rise to:
      Global symbol "$foo" requires explicit package name at hdhd line 10. Execution of hdhd aborted due to compilation errors.
      So no it does work. Update: but of course the string eval is different.

      /J\

Re^2: Making strict optional
by betterworld (Curate) on May 31, 2006 at 15:13 UTC
    The problem with that is that the following script compiles without complaints (even when strict is available) and prints "5".
    sub BEGIN { { eval 'use strict' } } $not_declared = 5; print $not_declared, "\n";
    "use strict" is always local for the surrounding block, so you cannot put it into eval or BEGIN.