Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: use of already eval()ed module (from string)

by Veltro (Hermit)
on Jan 09, 2019 at 11:17 UTC ( [id://1228248]=note: print w/replies, xml ) Need Help??


in reply to use of already eval()ed module (from string)

Hi bliako,

I think that something like this could work

#!/usr/bin/env perl use strict; use warnings; sub updateINC { my ( $modfn, $code ) = @_ ; # code = ref push( @INC, sub { my ( $coderef, $filename ) = @_ ; if ( $filename eq $modfn ) { # print STDOUT "########### filename = $filename\n" ; open( my $fh, '<', $code ) or die "Open Hello failed\n" ; return $fh ; } } ) ; } 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 updateINC('Test/Module/Hello/Hello.pm', \$mod1) ; 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 updateINC('Test/Module/Hello/Goodbye.pm', \$mod2) ; } 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, $@";

I haven't looked at any of the other solutions, so I don't know what is better or not. I just tried it out and it seems to work

I hope this may be of some help to you,

Veltro

Replies are listed 'Best First'.
Re^2: use of already eval()ed module (from string)
by bliako (Monsignor) on Jan 10, 2019 at 12:16 UTC

    Yes Veltro this works fine! Thank you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1228248]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-18 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found