Swandog has asked for the wisdom of the Perl Monks concerning the following question:
I have a feeling I know the answer to this, but I thought I would see if there's something I don't know.
For reasons beyond the scope of this question, I have a program that is eval-ing the content of a lot strings (well, files, but whatever). The problem is that a lot of those files have subroutines defined in them as helpers, and they often have the same name. So when I eval them one after another, I get a lot of "subroutine redefined" warnings.
Here's some code that essentially demonstrates the problem:
#/usr/bin/perl use warnings; use strict; sub eval_string { my $string=shift; eval $string; } my $string1=" sub foo { print qq(first definition\n); } foo(); "; eval_string($string1); my $string2=" sub foo { print qq(second definition\n); } foo(); "; eval_string($string2);
and the output:
~>perl test.pl first definition Subroutine foo redefined at (eval 2) line 2. second definition
I know I can get around this by declaring "no warnings 'redefine'" right before my eval, but I feel like that's a second-best solution. If someone did something really stupid and redefined a subroutine used outside the eval, I'd want to know that.
Obviously it would be easier if the strings created anonymous subroutines instead of defining named subroutines, but again, beyond the scope. What I'm wondering is if there's a way to tell eval to not, essentially, "import" any subroutines defined in the execution of the eval. Or, if there is an existing alternative to eval (on CPAN or some such) that does this for me.
If not, I can deal with this. I just thought I'd probe the mind of the community.
Thanks for any help!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Prevent import of subroutines from eval
by choroba (Cardinal) on Oct 10, 2013 at 22:24 UTC | |
by AnomalousMonk (Archbishop) on Oct 10, 2013 at 22:52 UTC | |
by Swandog (Initiate) on Oct 11, 2013 at 16:42 UTC | |
by choroba (Cardinal) on Oct 11, 2013 at 17:24 UTC | |
|
Re: Prevent import of subroutines from eval (fork or exec)
by tye (Sage) on Oct 11, 2013 at 02:35 UTC | |
by LanX (Saint) on Oct 11, 2013 at 14:24 UTC | |
by Swandog (Initiate) on Oct 11, 2013 at 16:44 UTC | |
|
Re: Prevent import of subroutines from eval
by LanX (Saint) on Oct 10, 2013 at 22:22 UTC | |
|
Re: Prevent import of subroutines from eval
by Anonymous Monk on Oct 10, 2013 at 22:18 UTC |