1

Is it possible to include/source another perl script, or launch it as a "sub"? This works, but looks ugly:

test2.pl:

print "I'm in test2.pl; args: @ARGV\n";

test.pl:

#!/usr/bin/perl

use File::Temp qw/tempdir/;
use File::Copy qw/copy/;

my $tmplib;

use lib ($tmplib = tempdir()) . (
  copy("./test2.pl", "$tmplib/test2.pm") ? "" : (die "$!")
  );

use test2;

x

$ ./test.pl a b c
I'm in test2.pl; args: a b c
1
  • 2
    Why would you want to do that, is the question that matters. This is a bad solution. Commented May 3, 2013 at 10:46

2 Answers 2

5

It sounds like you want the do operator, although it also sounds like very bad design.

This is what the documentation says.

do EXPR Uses the value of EXPR as a filename and executes the contents
        of the file as a Perl script.
            do 'stat.pl';

        is just like

            eval `cat stat.pl`;
Sign up to request clarification or add additional context in comments.

3 Comments

I need to debug a script under Eclipse and I want to make some preparations before the actual script is launched.
Breakpoints are not updated when a new module is loaded with 'do'.
If you are getting the right effect with use and not with do then it is probably because use is executed at compile time. Just write BEGIN { do 'test2.pl' } instead and it should work fine.
1

You can use do to run another Perl script in the same interpreter:

do 'test2.pl';

This will reuse the command line parameters from the outer script. To pass different parameters, you can override @ARGV locally, like:

{
    local @ARGV = qw(par1 par2 par3);
    do 'test2.pl';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.