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!
In reply to Prevent import of subroutines from eval by Swandog
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |