in reply to ARGV Usage
You could also use $ARGV[0], 1, etc, and use $#ARGV to know how many arguments you have. Another way is just to use shift:#!/usr/bin/perl -w # # input from command line # my ( $foo, $bar ) = @ARGV; print "foo = $foo\n"; print "bar = $bar\n";
Now, if you're serious about command line input, I'd suggest looking at Getopt::Long. and perhaps Pod::Usage. A piece of the beginning of one of my work scripts might look like:#!/usr/bin/perl -w # # input from command line # my $foo = shift; my $bar = shift; print "foo = $foo\n"; print "bar = $bar\n";
Update: cleanup and typo fixes#!/usr/bin/perl # # embed perdoc somewhere (top or bottom) for script documentation. # use warnings; use strict; use Getopt::Long; use Pod::Usage; my $help = 0; my $man = 0; GetOptions('help|?' => \$help, man => \$man ) or pod2usage(2); pod2usage(-exitval => 0, -verbose => 1) if $help; pod2usage(-exitval => 0, -verbose => 2) if $man; my $file = shift; pod2usage(-exitfile => 1, -verbose => 1) unless ( defined( $file )); die("File $file is not a regular file.\n") unless ( -f $file );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: ARGV Usage
by Fletch (Bishop) on Aug 30, 2007 at 15:31 UTC |