Solved

passing args in UNIX to Perl script?

Posted on 1999-01-07
5
278 Views
Last Modified: 2010-03-05
How do I pass arguments to a perl script in the UNIX shell?
0
Comment
Question by:tpchai
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:blu
ID: 1207377
The args to a perl script are stored in the array, @ARGV.

cat args.pl

#!/bin/perl

$i=0;
print "num of args=$#ARGV\n";
foreach (0..$#ARGV) {
        print "$i=$ARGV[$i]\n";
        $i++;
}

args.pl arg0 arg1 arg2 arg3
num of args=3
0=arg0
1=arg1
2=arg2
3=arg3

0
 

Author Comment

by:tpchai
ID: 1207378
Sorry, should have been a little more clear in my question. Say my perl script expects 2 name/value pairs, eg. on a web form <input type=text name=sex> <input type=text name=age>, how  do I simulate submiting this form with sex=male age=20 in the UNIX shell.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1207379
GET or POST?

You can pass them in environment variables, e.g.
QUERY_STRING="sex=male&age=2";
export QUERY_STRING
or, if you
use CGI.pm;
you can pass also them on the command line
args.pl sex=male age=2
0
 

Author Comment

by:tpchai
ID: 1207380
Ozo, thanks, perhaps you can submit that as an answer. What diff does it make whether it's GET or POST?
0
 
LVL 84

Accepted Solution

by:
ozo earned 20 total points
ID: 1207381
when $ENV{REQUEST_METHOD} eq 'GET', the data is passed in $ENV{QUERY_STRING}
when $ENV{REQUEST_METHOD} eq 'POST', $ENV{CONTENT_LENGTH} is the length of the data sent via <STDIN>
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question