Getopt Help

Hi All,

An old work friend wrote a script which I've been trying to understand how a section of it currently works and work out how i can add some command line switches which i can use later in the script to append the output depending on the command line arguements.

Currently it works by triggering is as per the below

./data_parser.pl -f testinputfile.txt
use strict;
use Getopt::Std;
use constant DEBUG => 0;

$| = 1;
my $current_colour = '36';

my %opt = ('f' => '-');

getopts("f:", \%opt);
my $opentag = $opt{'f'};
if ($opt{'f'} eq '-') {
        ;
} elsif (-f $opt{'f'}) {
        if ($opt{'f'} =~ /\.gz$/) {
                $opentag = 'gunzip -c '.$opt{'f'}.'|';
        }
} else {
        die "File ", $opt{'f'}, " not found\n";
}
open (FH, $opentag);

I dont understand what the getopts parts are doing, would someone be kind enough to shed some light and point me in the right direction with how i can add some switches on command execution.

Thanks in advance.

So -f is an option that takes an argument. Before the getopts() function is called a default value of '-' is put into the hash %opt so that it can be determined whether the -f is used or not.

You should be able to read up on this, and also determine how to add options, by reading the man page:

man Getopt::Std

Andrew