in reply to parsing arguments

(@_@) you are not using Getopt::Long yet??? I see that many people has pointed you in the direction of Getopt::Long. I will post one of the templates that I use when writing a new application in Perl.

#!/usr/bin/perl -w # My Perl Application # $Id$ # --- # $Log$ use Getopt::Long; use Pod::Usage; # recommended to use together with Getopt use strict; use vars qw($VERSION); $VERSION = sprintf( '%d.%02d', q$Revision$ =~ /(\d+)\.(\d+)/ ); # Parse command line arguments and assign corresponding variables # Arguments can be put into hash, array of scalar GetOptions ( 'i|in=s' => \( my @file_in = () ), 'o|out=s' => \( my $file_out = undef ), 'define=s' => \( my %defines = () ), 'verbose' => \( my $verbose = 0 ), 'version' => sub { die "$VERSION" }, 'quiet' => sub { $verbose = 0 } ); unless ( $#file_in > 0 && defined $file_out ) { pod2usage( -exitval => 1, -output => \*STDERR ); } # Beginning of application code .... __END__ =pod =head1 NAME application.pl =head1 SYNOPSIS application.pl [options] =head1 ARGUMENTS Arguments to this script are mandatory. =over 4 =item B<-i|--in [file]> Specify an input data file for report extraction. =item B<-o|--out [file]> Specify the output file name for the report. =back =cut
In the above example, you can pass in multiple values of -i option with -i f1 -i f2 -i f3, etc.

You can also pass in, say, -o "o1,o2,o3" and then do

@output_files = split /,/, $file_out;


Replies are listed 'Best First'.
Re: Re: parsing arguments
by mifflin (Curate) on Oct 07, 2003 at 23:58 UTC
    Yes, this looks nice but I don't have the option of changing the original ksh scripts that would pass the arguments into the new perl program. Maybe I didn't understand your example enough but I still don't see how I can use it. Thanks though. It was a great example in how to use the Getopt::Long module :)
      Ok, you can use a one liner to transform the arguments into key=value pairs in a hash table.
      #!/usr/bin/perl -w use strict; use Data::Dumper; my %arg = map{my($key,$val)=split/=/;$key=>$val}@ARGV; print Dumper(\%arg);
      And when running this with the command-line parameters in your example, I got the following result:
      $VAR1 = { 'lexwhere' => '\'in(\'DIS\', \'DIM\', \'DIR\')\'', 'title' => '\'this is a title\'', 'vchtyp' => 'I', 'jrnltyp' => 'D' };