Solved

passing args in UNIX to Perl script?

Posted on 1999-01-07
5
287 Views
Last Modified: 2010-03-05
How do I pass arguments to a perl script in the UNIX shell?
0
Comment
Question by:tpchai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

628 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