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

In reply to Re: Can you create another perl interpreter within the current script? by Sinister
in thread Can you create another perl interpreter within the current script? by broquaint

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.