http://www.perlmonks.org?node_id=376724

dpuu has asked for the wisdom of the Perl Monks concerning the following question:

I have a file that we're including via C<do>. I've has a request to impose strictness and warnings on that file. Warnings are easy ($^W); but stricness is more difficult.

I could obviously slurp the file into a string and prepend a "use strict;" declaration before eval-ing it. I'd have to fix-up the filename in any error messages ("eval 347" is not a very meaningful), but it would be do-able.

The other way I can think of is to do the same prepending, but do it by messing with the filename passed to C<do>:

do qq(cat $filename | perl -ple 'BEGIN { print "use strict;" }');
This doesn't seem ideal, either. Is there a simpler way?

--Dave
Opinions my own; statements of fact may be in error.

Replies are listed 'Best First'.
Re: imposing "strict" on an included file
by gaal (Parson) on Jul 22, 2004 at 22:22 UTC
Re: imposing "strict" on an included file
by BUU (Prior) on Jul 22, 2004 at 23:58 UTC
    Why are you trying to do this? If the library is completely written, working and out of your control, then adding strict and warnings won't cause it to do anything it doesn't already do, except for maybe stop working in new and stupid ways. Otherwise, if the module is still in development, which is the only time strict is useful, why don't you just make whoever is developing it put strict in?
      The files in question define tests for our hardware. In hardware testing, false-passes are the ultimate evil. A false fail can be dealt with (you just get someone to fix it), but a false-pass can make its way into production, un-noticed. Therefore, we'd far rather break modules that are currently seming to work, then to leave them with potentially hidden problems. Adding "use strict" is seen as a bit of low-hanging fruit.

      --Dave
      Opinions my own; statements of fact may be in error.

        So why not just open up the files in question and type "use strict" at the top?
Re: imposing "strict" on an included file
by pg (Canon) on Jul 22, 2004 at 21:48 UTC

    Your approach does not always work. Try this piece of code:

    use strict;#suppose this line is added by you no strict; use warnings; $a = "a"; &$a; sub a { print "abc"; }

    This piece of code works with no problem, which disproves your way. You have to check wehther your "use strict" is cancelled in any way.