Solved

passing args in UNIX to Perl script?

Posted on 1999-01-07
5
270 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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