Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Can you create another perl interpreter within the current script?

by Sinister (Friar)
on Mar 04, 2002 at 22:38 UTC ( [id://149249]=note: print w/replies, xml ) Need Help??


in reply to Can you create another perl interpreter within the current script?

Took me a little while, cause I am no closures and eval hero, but here is the trick...

This is the script holding your routines:
sub hello { return "Hello"; } sub world { return "world!"; } sub foo_bar { my ($foo, $bar) = @_; print "$foo $bar\n"; }

And this is the interpreter.
use strict; # Try to load code from a different perl script. Put it in a closure # and run run run... my $load_file = $ARGV[0]; # $flags my $in = 0; my @subs = (); my %routines = (); my %sub = (); open(IN, "<$load_file"); while (<IN>) { if ( /sub (\w+) \{/ ) { # hey! A routine has begun... $in = 1; push @subs,$1; $routines{$1} = ""; } if ( $in == 1 ) { if ( /\}/ ) { # sub ends. $in = 0; } else { s/^\s+//g; $routines{$subs[$#subs]} .= $_ unless /sub/; } } } close(IN); # mmm - we now have the internals of the routines in core. # let's see what we can do with them. print "You have these subs:\n"; foreach my $subs (keys %routines) { eval("\$sub{$subs} = sub { " . $routines{$subs} . " };"); print "\$sub{$subs} = sub { " . $routines{$subs} . " };\n" } $sub{'foo_bar'}->($sub{'hello'}->(), $sub{'world'}->()),"\n";


To be honest with you - I am kind of proud of myself *blush blush*

A righthanded-lefthand....
"Field experience is something you don't get until just after you need it."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found