gyre007 has asked for the wisdom of the Perl Monks concerning the following question:

Hi I have a problem with my script. When I run it like this: FieldCompare.pl -t sur out20111_23866.sorted SUR_P1023866.sorted -o results it EXITS with the following statement: **** Specify name of the output file! ***** But I passed in -o parameter called "results" SO WHY DOES $opt_o" variable NOT contain "results" (or any other parameter I pass to that option) string ? Thanks!
#!/usr/bin/perl -w use strict; use Getopt::Long; use Getopt::Std; #ArrayS of hashes my @MainFields = (); my @ReservedINFO = (); my $output_type; my $outputFile; our($opt_h, $opt_c, $opt_t, $opt_o); getopts('hc:t:o:'); # Reading specified options and calling particular initialization func +tions sub Init { my $file1; my $file2; my $tmp; if(defined($opt_h)){ print Syntax(); exit; } if(!defined($opt_t)){ print "\n***** Specify the type of the output! *****\n"; print Syntax(); exit; } $output_type = $opt_t; $file1 = $ARGV[0]; $file2 = $ARGV[1]; if((!defined($file1)) || (!defined($file2))){ print "\n***** Specify the INPUT files! *****\n"; print Syntax(); exit; } print "\nFirst file - $file1\nSecond File - $file2\n"; if(!defined($opt_c)){ print "\nNo config file provided, defualt configuration will be us +ed!\n"; init_default_config($output_type); } else { read_config_file($output_type); } if(!defined($opt_o)){ print "\n***** Specify name of the output file! *****\n"; print Syntax(); exit; } $outputFile = $opt_o;
I mean if THE VERY SAME CODE WORKS FOR 'T' OPTION why DOES IT NOT WORK for 'O' option ?

Replies are listed 'Best First'.
Re: handling scripts options
by Fletch (Bishop) on Oct 15, 2008 at 12:39 UTC

    Getopt::Std stops looking for arguments at the first non-argument argument (i.e. an argument that doesn't start with a dash). If you use Getopt::Long instead (which you've already got used anyhow for some reason . . .) it should behave more like you expect.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: handling scripts options
by dreadpiratepeter (Priest) on Oct 15, 2008 at 12:37 UTC
    You can't put named options after the positional ones, try
    -t sur -o results out20111_23866.sorted SUR_P1023866.sorted


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."