Dear Monks,

I have come across a situation where I have a module and a test script which uses it and all is fine when done using the perl interpreter from the command line. However, when I read the module file into a string and the test script into another string with the intend to eval() the strings one after the other, the operation collapses at the eval() of the test script string.

After some time, I realised why I was getting the error Can't locate Module.pm in @INC at the second eval which indeed has a use Module; statement. Obviously Module.pm is not in @INC and any use statement for it will fail even if said module has already been eval'ed successfully.

So, I am asking whether there is an easier solution to this kind of problem than analysing each script with PPI and removing use/require statements ONLY of modules already eval'ed. Because obviously there will be some use/require statements in there which must remain as they refer to modules which should be in @INC. I am doing the PPI way right now but I hope there is a more natural way.

Here is a test which demonstrates the problem when the marked statements uncomment:

#!/usr/bin/env perl use strict; use warnings; sub load_modules { my $mod1 = <<'EOM'; package Test::Module::Hello::Hello; our $VERSION = 0.1; use strict; use warnings; sub go { return "Hi iam ".__PACKAGE__ } 1; __END__ EOM my $mod2 = <<'EOM'; package Test::Module::Hello::Goodbye; our $VERSION = 0.1; use strict; use warnings; # >>> This will kill the eval if uncommented #use Test::Module::Hello::Hello; sub go { return "Hi iam ".__PACKAGE__ } 1; __END__ EOM eval($mod1) or die "$mod1\n\neval failed $@\n"; eval($mod2) or die "$mod2\n\neval failed $@\n"; } load_modules(); my $testscript = ' # >>> Following use/require will kill the eval #use Test::Module::Hello::Hello; require Test::Module::Hello::Goodbye; my $ret = Test::Module::Hello::Hello::go(); print "ret=$ret\n"; $ret = Test::Module::Hello::Goodbye::go(); print "ret=$ret\n"; '; eval($testscript) or die "eval failed, $@";

thanks, bliako


In reply to use of already eval()ed module (from string) by bliako

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.