Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-23 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found