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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.