tags:

views:

308

answers:

2

I am trying to collect the values from command line using Getopt::Std in my Perl script.

use Getopt::Std;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
getopts('i:o:p:');
my $inputfile = our $opt_i;
my $outputfile = our $opt_o;
my $parameter_value = our $opt_p;

Here the first two variables ($inputfile,$outputfile) are mandatory but the last variable ($parameter_value) is optional and can be ignored.

I am trying to set some value by default to the last variable ($parameter_value) when the -p flag is ignored at the command line.

I tried using this:

my $parameter_value = our $opt_p || "20";

Here its passes the correct value when -p flag is ignored at command line. But the problem is when I am providing some value from the command line (for instance -p 58), the same value 20 is passed to the program instead of 58 which I passed from command line.

Can you please help me out by pointing the mistakes I am making here?

Thank you.

+10  A: 

The best thing is to use GetOpt::Long and use a hash instead of individual variables. Then you can pass default values by pre-populating the array

    use Getopt::Long;
    my %opts = (parameter => 20);
    GetOptions( \%opts, 
            'p|parameter=i', 
            'o|outputfile=s',
            'i|inputfile=s'
    ) or die "Invalid parameters!";

    # I didn't bother cloning STANDARD_HELP_VERSION = 1;
DVK
Thanks brian d foy it works now :)
Suren
;) ;) ;) ;) ;) ;)
DVK
Sinan - (1) You just destroyed the joke :-( ; (2) Do I get a badge for whole two Perl luminaries editing my post in the same day? :)
DVK
+5  A: 
#/usr/bin/perl

use strict;
use warnings;

use Getopt::Std;

getopts('i:o:p:');
our($opt_i, $opt_o, $opt_p);

my $inputfile = $opt_i;
my $outputfile = $opt_o;
my $parameter_value = $opt_p || "20";

print "$_\n" for $inputfile, $outputfile, $parameter_value;
C:\Temp> ks -iinput -ooutput -p55
input
output
55
C:\Temp> ks -iinput -ooutput
input
output
20
Sinan Ünür
Good one :) = +1
DVK
thanx Sinan, This works very much fine and retains the format of my coding. Thanx a ton.
Suren
If you're using 5.10+ it would be better to use `//` instead of `||` because the latter tests for truth, not defined-ness. It will prevent you from being able to pass in false values (e.g. 0). The long-winded way to get equivalent behavior prior to 5.10 is `$x = defined $y ? $y : Z` where `Z` is the default value.
Michael Carman
@Michael Carman I guess I assumed that 0 was not a valid value.
Sinan Ünür
So, I take it there was some problem with using `our` within the assignment statement itself, and using it beforehand fixes it? Could you expand on what the difference is between your code and Suren's?
Rob Kennedy
@Rob Kennedy In terms of posted code, the placement of `our` would not make a difference. All I can deduce is that there is something else going on in his `strict`-free, `warnings`-free code which caused the latter `our` to be within the scope of a different package than the one set by `getopts`.
Sinan Ünür