Operating System - HP-UX
1819500 Members
3164 Online
109603 Solutions
New Discussion юеВ

Perl: Command Line Arguments

 
SOLVED
Go to solution
Tom Dawson
Regular Advisor

Perl: Command Line Arguments

Hi,

Does anyone have an example of passing two or three command line arguments to a Perl script? I've searched the ITRC and found plenty examples of passing arguments to a Perl subroutine, but not to the Perl script itself. i.e. From a POSIX or Bourne shell script or directly from the crontab file.

It's probably indicative of how little I know about PERL. I've tried taking the "subroutine" examples and using them. But I can't get them to work.

Thanks,
Tom
4 REPLIES 4
harry d brown jr
Honored Contributor
Solution

Re: Perl: Command Line Arguments

#!/usr/contrib/bin/perl
#
#
$DataFileName = $ARGV[0];
$searchstr = $ARGV[1];
$valuestr = $ARGV[2];
print "filename is >>".$ARGV[0]."<< searchstr is >>".$ARGV[1]."<<\n";
print "====================\n";


live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor

Re: Perl: Command Line Arguments

This might be useful also:

http://members.shaw.ca/andrew-johnson/perl/archit/Oct00

live free or die
harry
Live Free or Die
H.Merijn Brand (procura
Honored Contributor

Re: Perl: Command Line Arguments

As harry sais, all arguments passed to a perl script are available in the global array @ARGV.
A major difference with programming in C is that $ARGV[0] is the first argument, and not the program name. Perl has no $ARGC, but since $#ARGV is available as the last defined index in @ARGV, that should not prove to be a problem. Using @ARGV in scalar context

$argc = @ARGV;
or
print "I have ", scalar @ARGV, " arguments\n";

will return the number of arguments passed to the script. Look at $0 and/or $^X to see how the script was called.

Some options can be set to variables implicitely without using @ARGV. See 'man perltun' for the details of using -s. Another thing to be aware of is the way perl scans the hashbang line

#!/usr/bin/perl -ws

passes the -w and -s options to the script without you having to type them.

HTH. Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Tom Dawson
Regular Advisor

Re: Perl: Command Line Arguments

Harry, Merijn,

Thanks! I knew I should be using the ARGV array, but I just couldn't get the syntax right.

Tom