Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using both ARGV and CGI in a Perl script

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];
like image 790
raz3r Avatar asked Nov 21 '11 16:11

raz3r


People also ask

Is Perl CGI client side or server side?

CGI refers to server-side execution, while Java refers to client-side execution. There are certain things (like animations) that can be improved by using Java. However, you can continue to use Perl to develop server-side applications.

Is Perl used for CGI?

While CGI scripts can be written in many different programming languages, Perl is commonly used because of its power, its flexibility and the availability of preexisting scripts. sent to the server through CGI, the results may be sent to the client.

How do I use argv in Perl?

Perl command line arguments stored in the special array called @ARGV . The array @ARGV contains the command-line arguments intended for the script. $#ARGV is generally the number of arguments minus one, because $ARGV[0] is the first argument, not the program's command name itself.


1 Answers

With CGI.pm you can pass params on the command line with no need to change your code. Quoting the docs:

If you are running the script from the command line or in the perl debugger, you can pass the script a list of keywords or parameter=value pairs on the command line or from standard input (you don't have to worry about tricking your script into reading from environment variables)

Wrt your example, it's a matter of doing:

perl script.cgi username=John
like image 105
larsen Avatar answered Sep 30 '22 15:09

larsen