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

I'm new with Perl, first time trying to use command line arguments and options. I want to provide the user with the options if you say "--help" : the "usage" comes up (doesn't run script) "--delete" : delete old files "--error" : print errors in log file then it will have 3 arguments so the command line would look like this:

learning_perl/script > program.pl --help --delete --err_dup F68 F100 5

this is my code for getting the command line but when I run this, I get an error and I know it has to do with the %options hash, but I'm not quite sure how to fix it

sub getCmndArg { # my $num_args = $#ARGV + 1; # if ($num_args != 3) { # print "\nUsage: csv_ext_utl -d begin_addr end_addr numberOfSi +ms\n"; # exit; # } my %options=(); GetOptions(\%options, qw[ help dfiles err_dup ]); if ($options{"help"}){ print "help is called - Help Panel:\n"; } else { print "help is NOT called\n"; } if ($options{"dfiles"}){ print "dfiles is called - deleting old simulation files\n"; my $unlinked = unlink glob {"address_register_sweep_*.sv"}; } else { print "dfiles NOT called\n"; } if ($options{"err_dup"}){ print "err_dup is called\n"; } else { print "err_dup NOT called\n"; } #$deleteFile = $ARGV[0]; $beginAddrHex = $ARGV[0]; $endAddrHex = $ARGV[1]; $numberOfSims = $ARGV[2]; }

Error I receive:

help is called - Help Panel: dfiles is called - deleting old simulation files Odd number of elements in anonymous hash at ./csv_ext_utl line 90 (#1) (W misc) You specified an odd number of elements to initialize a h +ash, which is odd, because hashes come in key/value pairs. err_dup NOT called

If anyone can chime in, I'd appreciate it! Thank you!

Replies are listed 'Best First'.
Re: PERL - newbie: first time working with GetOptions error given when running script
by Your Mother (Archbishop) on Oct 03, 2014 at 16:51 UTC

    You could just add an exit

    print "help is called - Help Panel:\n"; exit;

    I’d recommend looking at Pod::Usage instead of trying to roll this kind of thing though. Working stub–

    # File named "my-script-name.pl" to match Pod. use strict; use warnings; use Getopt::Long; use Pod::Usage; my $ok = GetOptions( help => \my $help, task => \my $do_something ); pod2usage( -verbose => 0 ) unless $ok; pod2usage( -verbose => 2 ) if $help; print "Doing... ", $do_something ? "something or other\n" : "NOTHING!\n"; __DATA__ =head1 Synopsis my-script-name.pl -help my-script-name.pl -task =head1 License WTFPL (NSFW!). =cut
Re: PERL - newbie: first time working with GetOptions error given when running script
by toolic (Bishop) on Oct 03, 2014 at 16:14 UTC
    You're not showing us the relevant code. What is line 90? Get rid of all the code you don't need, and add just the relevant code which demonstrates your problem (http://sscce.org).

    When I add this to the top of your code

    use Getopt::Long; getCmndArg();

    and run it, I get:

    program.pl --help --dfiles --err_dup F68 F100 5 help is called - Help Panel: dfiles is called - deleting old simulation files err_dup is called
Re: PERL - newbie: first time working with GetOptions error given when running script
by mr_mischief (Monsignor) on Oct 03, 2014 at 19:40 UTC

    You're use'ing Getopt but you aren't actually using it.

    The following code has absolutely no place in your program if you're using Getopt::Long:

    $beginAddrHex = $ARGV[0]; $endAddrHex = $ARGV[1]; $numberOfSims = $ARGV[2];

    You should be letting GetOptions do the work of getting the options. That's its reason for being. Your line should probably look more like this.:

    GetOptions( \%options, qw[ help+ delete+ err_dup=s{3} ] );

    This also means your %options hash should look something like this (if you believe in pre-populating it).:

    my %options = ( 'help' => 0, 'delete' => 0, 'err_dups' => [], );

    And then your if branch for err_dup would be more like this.:

    if ( $options{ 'err_dup' } ){ print "err_dup is called\n"; my ( $beginAddrHex, $endAddrHex, $numberOfSims ) = @{ $options +{ 'err_dup' } }; # or just pass the option straight to the sub you're + about to call } else { print "err_dup NOT called\n"; }

    Why you'd use GetOptions to make all the options numeric flags then try to pick apart @ARGV (with the wrong indexes, BTW) is beyond me. Just follow the docs and use string options and repetition specifiers (called a "repeat specifier" in the docs). You can also specify an array type if you're not sure how many you'll need.

    Oh, and since you're using hex you could enforce option type checking if you're willing to enter your hex values as 0xf68 and 0xF100 and such.:

    GetOptions( \%options, qw[ help+ delete+ err_dup=o{3} ] );
      Ohh, the three number values have nothing to do with the options, I'd want those either parameters to go into ( $beginAddrHex, $endAddrHex, $numberOfSims ) either ways. I thought the GetOptions would just see if any or all of the three options are enabled/disabled. Then after it would take the arguments, so even if "err_dup" is disabled I'd get the values.
Re: PERL - newbie: first time working with GetOptions error given when running script
by RTBailey (Novice) on Oct 04, 2014 at 15:59 UTC
    PerlWizard at www.rtbaileyphd.com/perlwizard is a free wizard for automatic Perl software code generation using simple forms. One of its main features is that it uses GetOpt::Long. You could just use that and everything will be handled for you. In any case, you can look at some of the examples at www.rtbaileyphd.com/downloads to see how I employed GetOpt::Long.