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?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With