Perl - embed a perl interpreter in a Perl program


    use Perl;

    my $p = Perl->new;
    print $p->eval(q/"Hello" . " " . "world" . "\n"/);


A Perl object represents a separate perl interpreter that you can manipulate from within Perl. This allows you to run other scripts without affecting your current interpreter, and then examine the results.



This creates and initialises a new perl interpreter, and returns an object through which you can manipulate it. Paramaters are


This sets the arguments for the perl interpreter, as passed to "perl_parse" in perlembed. An initial argv[0] of "perl" will be added automatically, so don't try and include one. If no arguments are passed, a single argument of -e0 will be used.


This will add appropriate -M arguments to the argv of the new interpreter. These will be added before any args given with ARGV.


This will pass appropriate -I arguments to the interpreter, before those from either ARGV or USE. If the ARRAYREF is not specified, it will pass in the current @INC, omitting entries that are references.

If the creation fails, it returns undef.


This invokes "perl_run" in perlembed on the interpreter, which will run the program given on the command line in Perl->new, if any. END blocks will be run at the end of this, so if you install any you must make sure to call this.


This evals EXPR in the other interpreter, and returns the string value of the result.


This returns the result of evaluating EXPR in the other interpreter. Results are passed back using Data::Dumper, and if anything is returned that cannot be frozen an exception will be thrown. Any exceptions thrown will be caught in $@ and undef returned, as with normal eval.

The EXPR will be evaluated in the same context (list, scalar, void) as eval is called in.

This will croak if Data::Dumper cannot be required in the sub-interpreter.

Note that you may wish to use _eval instead for more control over the other interpreter.



This will construct an expression that evaluates to a deep clone of EXPR, suitable for interpolating into a string to pass to $perl->eval.

EXPR will be evaluated in scalar context.



This is a tied hash which returns the result of calling Perl::make_expr on the given key: it makes it easier to interpolate into strings.


This is used to determine whether or not Data::Dumper should attempt to deparse CODE refs. Note that you need a new enough version of Data::Dumper for this to have any effect.



If this variable is set in the environment, copious amount of debugging info will be produced on STDERR. This is almost certainly of no use to anyone but me.


Anything that can't be Dumped, in particular filehandles, can't be passed between interpreters.

There are crashes in t/6threads.t with both AS Perl and MinGW perl. I don't fully understand why they occur, but I suspect a bug in perl.


Gurusamy Sarathy <>

Modified and updated for 5.8 by Ben Morrow <>


This program is distributed under the same terms as perl itself.