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

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

Orthodox Brothers in Perl and Strict Sisters of the Script,

Is there a way to enforce strictness on files included with do $filename?

Replies are listed 'Best First'.
Re: Can I force strictness on included files?
by broquaint (Abbot) on Jun 30, 2005 at 08:59 UTC
    If you're not adverse to a little module usage then Module::Locate can help you out:
    use Module::Locate 'get_source'; sub use_mod_strictly { my $mod = shift; my $ret = eval "use strict; " . get_source($mod); die $@ if $@; return $ret; }
    So this will grab the source of $mod (and populate %INC), add strictness to the source and finally evaluate the new strict source, checking eval for errors too.
    HTH

    _________
    broquaint

Re: Can I force strictness on included files?
by jonadab (Parson) on Jun 30, 2005 at 10:06 UTC

    Are you sure you want to do that? I can't recommend it. As a rule, code that wasn't *designed* to run under strict, won't. Put another way, if the files in question don't have use strict in them, they probably also don't have their variables all predeclared and so on and so forth. It takes rather significant editing to make code of any nontrivial level of complexity run under strictures. Adding the strict declaration to it is, by comparison, a very minor additional thing to do.

      Good point.

      In my case, however, it is code that was designed to be run under strict. It is not any random code from outside our control, it is data files (that just happen to be in Perl) consisting of a single hashref or a single coderef. That the use strict is not in there is mere lazyness and forgetfulness (two of a Perl programmer's virtues).

      Unless there is a switch to turn on strict for all files, I think I will settle for the solution of slurping and eval'ing the file manually, adding the "use strict" in the process, as suggested by broquaint.

        If this is the case then I can't see what adding the stricture to the code will buy you, to be honest I wouldn't even bother.

        /J\

Re: Can I force strictness on included files?
by tlm (Prior) on Jun 30, 2005 at 11:45 UTC

    This is not an answer to your question, but rather a related observation. Here's the relevant section of the docs for do (my emphasis):

    • do EXPR

      Uses the value of EXPR as a filename and executes the contents of the file as a Perl script. Its primary use is to include subroutines from a Perl subroutine library.
      do 'stat.pl';
      is just like
      eval `cat stat.pl`;
      except that it’s more efficient and concise, keeps track of the current filename for error messages, searches the @INC libraries, and updates %INC if the file is found. … It also differs in that code evaluated with "do FILENAME" cannot see lexi­cals in the enclosing scope; "eval STRING" does.
    It may not be not entirely obvious, but the underlined sentence implies that, despite the similarity of the two constructs, with do 'stat.pl' strictures are not enforced, but they are with eval `cat stat.pl`.

    So you'll need to use eval instead of do if you want strictures, but you'll have to check $@ yourself after the eval.

    the lowliest monk

Re: Can I force strictness on included files?
by monkfan (Curate) on Jun 30, 2005 at 08:09 UTC
    I'm not quite sure what do you mean by that.
    From what I understand I guess you just need to include 'use strict' in the file you wish to call. Like this:
    use strict; #File name: mydata.pl my $res = {# reference variable value..etc};
    Then call it with:
    my $another_var = do 'mydata.pl';
    Please see tlm's answer to my posting in the same breath with your question.
    Regards,
    Edward
      From what I understand I guess you just need to include 'use strict' in the file you wish to call.

      Sure, but I want to avoid having to do this.

      I like to update things in only one place, so instead of specifying use strict; in my about one hundred data files (I think I am actually in a similar situation than you describe), I would rather do that in the one place where all these other files are included in the main module.

Re: Can I force strictness on included files?
by displeaser (Hermit) on Jun 30, 2005 at 14:01 UTC
Re: Can I force strictness on included files?
by Roy Johnson (Monsignor) on Jun 30, 2005 at 15:42 UTC
    If you can edit the files, put in use strict. If you can't edit the files, it's not going to help you much to know that they have stricture problems.

    Caution: Contents may have been coded under pressure.