I have two Perl scripts. Let's call them one.pl
and two.pl
.
one.pl
processes some data and needs to call two.pl
, passing a variable.
In one.pl
I can
require "two.pl"
to call the second script.
That works fine, but I want to pass a CGI variable to two.pl
.
Is there any way to do this without rewriting two.pl
as a Perl module?
For example what I want is:
...
require "two.pl $number";
use CGI;
my $cgi = new CGI;
my $number = $cgi->param('number');
...
EDIT: two.pl
should only ever be called once
If your one.pl is a shell script, you can set @ARGV
before the call to require
. This abuses CGI's mode to work with command line arguments. The arg needs to be the param name equals the value.
{
my $number = 5;
local @ARGV = ( "number=$number" );
require "two.pl";
}
The key=value
format is important. The local
keyword makes sure that @ARGV
is only set inside the block, making sure other possible arguments to your script are not permanently lost, but rather invisible to two.pl.
Note that for both of these you can only ever require
a script once. That's the idea of require
. Perl keeps track of what it's loaded already (in %INC
). If you are in an environment like mod_perl
, or a modern Perl application that runs persistently, you should use do "two.pl"
instead, which will execute it every time. But then that might break other things if two.pl is not designed to be ran multiple times in the same process.
Your best bet is to refactor the code inside two.pl into a module, and use that in both scripts.