jujiro_eb has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

Need your help again. Is it possible to launch dynamically generated perl statements from within a script, which reference the variables of the parent script? In other words, execute the generated statements without invoking another copy of the interpreter.

For example:

I have the parent script as following:

#! /usr/bin/perl use strict; # Example of launching a perl script from within another script my $v1="Hello"; my $v2="There"; # I do know that the following statement will not produce the desired +result. system ("perl launch_child.pl");

Dynamically generated child script, launch_child.pl looks like this:

print "$v1\n"; print "$v2\n";

Thanks.

Ash

Replies are listed 'Best First'.
Re: Help: Launch generated perl statements from a perl script
by almut (Canon) on Apr 17, 2009 at 21:31 UTC

    If I'm understanding you correctly, you're looking for eval.

    open my $fh, "<", "launch_child.pl" or die $!; my $script = join '', <$fh>; close $fh; eval $script;

    (In contrast to do, it does see lexicals in the enclosing scope.)

Re: Help: Launch generated perl statements from a perl script
by morgon (Priest) on Apr 17, 2009 at 21:38 UTC
    If you declare your variables as package-variables then you can use "do", e.g.:
    use strict; our $v1="Hello"; our $v2="There"; do "launch_child.pl";
    See "perldoc -f do".