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

Running my script with -smm option always prints "1",any idea </P

c:\perl test.pl -smm c:\files

#!/usr/bin/perl -w use strict; use warnings; use Getopt::Long; my %options=(); GetOptions (\%options, 'smm','dmm') or die("\nERROR:Invalid Option\n") +; print "\nOPTION MSM:$options{msm}\n"; #always prints as one

Replies are listed 'Best First'.
Re: Options always prints 1
by BrowserUk (Patriarch) on Apr 21, 2011 at 23:41 UTC

    Since you haven't specified that -smm should take a value, it is being taken as a "simple" (ie. boolean) option. And as you've supplied it on the command line, it will be set to true. And true is often represented as 1 in Perl.

    If your intent is that c:\files should be taken as the value for -smm, the you should specify that as -ssm=s in te call to GetOptions().


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Options always prints 1
by ikegami (Patriarch) on Apr 21, 2011 at 23:45 UTC
    See the Options With Values section of the documentation.
    GetOptions(\%options, 'msm=s')

    Without the =s, "the option is considered boolean, a value of 1 will be assigned when the option is used on the command line."

Re: Options always prints 1
by educated_foo (Vicar) on Apr 21, 2011 at 23:16 UTC
    The script you posted is highly unlikely to print 1 (smm != msm != dmm). Beyond that, you might try reading the manual for Getopt::Long, where it describes how to declare options that take arguments.