I am writing a Perl script that can run both from command line and from a web page. The script receives a couple of parameters and it reads those parameters through $ARGV if it started from command line and from CGI if it started from a web page. How can I do that?
my $username;
my $cgi = new CGI;
#IF CGI
$username = $cgi->param('username');
#IF COMMAND LINE
$username = $ARGV[0];
The cleanest way might be to put the meat of your code in a module, and have a script for each interface (CGI and command line).
You could test for the presence of CGI environment variables (
$ENV{SERVER_PROTOCOL}) to see if CGI is being used, but that would fail if the script is used as a command-line script from another CGI script.If all you want to pass via
@ARGVare form inputs, keep in mind that CGI (the module) will check the@ARGVfor inputs if the script is not called as a CGI script. See the section entitled “DEBUGGING” in the documentation.