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.
| [reply] [d/l] |
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.
| [reply] [d/l] |
|
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.
| [reply] [d/l] [select] |
|
| [reply] |
|
|
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):
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.
| [reply] [d/l] [select] |
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.
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] |
Re: Can I force strictness on included files?
by displeaser (Hermit) on Jun 30, 2005 at 14:01 UTC
|
| [reply] |
Re: Can I force strictness on included files?
by Roy Johnson (Monsignor) on Jun 30, 2005 at 15:42 UTC
|
| [reply] [d/l] |