Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"do" function in Perl

Tags:

perl

Recently, I have a perl script which calls another perl script using "do"; ie.

do "./script2.pl $arg1 $arg2";

My script2.pl was designed to consume 2 arguments.

However, calling script2.pl in the way above will not pass the arguments into script2.pl.

I know there is another way using "system", but my script1.pl needs to get some variable values through "require".

Can I actually use "do" and pass in the arguments to my script2?

like image 366
GJ. Avatar asked Sep 25 '26 11:09

GJ.


1 Answers

If the script you are calling is expecting arguments, it will expect them in the variable @ARGV. If you are using do, the @ARGV in the calling script will be visible in the called script.

{
    local @ARGV = ($arg1, $arg2);
    do './script2.pl';
}

Using local like this will restore the old values of @ARGV at the end of the block, kind of like saying

@ARGV_old = @ARGV;
@ARGV = ($arg1,$arg2);
do './script2.pl';
@ARGV = @ARGV_old;
like image 179
mob Avatar answered Sep 27 '26 11:09

mob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!