Solved

passing args in UNIX to Perl script?

Posted on 1999-01-07
5
263 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
perl split by | 2 80
Port 80 requests 16 79
Perl string replace for refred url 9 57
PERL variable conundrum 9 83
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 …
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now