How can I set default values using Getopt::Std?

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;

#/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

Tags:

Perl

Getopt