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

For some reason I'm unable to get the simplest getopt functionality to work. I'm fine with Getopt::Long, but Getopt::Std for some reason isn't doing what I think it should:

eg:

#!/usr/local/bin/perl -w use strict; use Getopt::Std; my $opt_u = 0; getopt('u') || die "can't do getopt\n"; print "username is $opt_u\n";

This ALWAYS dies. If I don't use die, it just returns an empty variable. It seems to be me misunderstanding how to declare the variable while using strict. Without declaring the variable, the script won't run. But the only way to get it to run is to not use strict and not declare it.

###

Ok, I've played round and found the solution is to declare it within the package. Like so:

#!/usr/bin/perl -w use strict; use Getopt::Std; $Getopt::Std::opt_u = 0; getopt('u'); print "username is $Getopt::Std::opt_u\n";

Thanks for the help, everyone.

toeslikefingers.com - because you've got too much time on your hands

janitored by ybiC: Retitle from less than descriptive "dumbass strict / getopt questions"

Replies are listed 'Best First'.
Re: Getopt::Std dies or returns empty var
by derby (Abbot) on Mar 11, 2004 at 13:10 UTC
    Take a look at the documentation (Getopt::Std). The opt_x either need to be global or if you don't like globals, the second arg is a reference to a hash that will contain the options. The getopt function doesn't have a return value.

    #!/usr/local/bin/perl -w use strict; use Getopt::Std; our( $opt_u ); getopt('u'); die "no u option passed\n" unless $opt_u; print "username is $opt_u\n";

    -derby

      Interesting. This works. I found some docs saying you had to use 'our' earlier, but the first time I tried running it 5.00503, it bitched about it being deprecated. Just ran it on 5.8 though and it worked. I'd assumed that if an earlier version barfed it, the newer one must be deprecated. This sucks too, cause the system I need it to run on can't really be upgraded :S

      toeslikefingers.com - because you've got too much time on your hands
        the system I need it to run on can't really be upgraded

        In that case, rather than declaring the variable with our $opt_u you should (must) declare a package variable with use vars qw/$opt_u/.

        Compatibility with older Perl interpreters is the main reason why I don't use our in my code.

Re: Getopt::Std dies or returns empty var
by Happy-the-monk (Canon) on Mar 11, 2004 at 13:07 UTC

    funny thing, I have never looked at it before. This works:

    use Getopt::Std; getopt('u'); print "username is $opt_u\n";

    it breaks as soon as you do either one:

    • use strict;
    • predeclare $opt_u
    • use the   || die   idiom

    Sören

Re: Getopt::Std dies or returns empty var
by matija (Priest) on Mar 11, 2004 at 13:11 UTC
    You are using use strict;, but you want to use the $opt_var form of assignment.

    The getopt routine can't assign to a myvariable that has it's scope in your code. Two possible solutions:

    • Declare the variable as our $opt_u;
    • getopt('u',\%args); and check $args{'u'}
      It quite misleading to mention "use strict". The OP's code isn't failing because of the "use strict" - it will still fail if you remove the "use strict". The problem is the lexical declaration of "$opt_u" - that one is not available to "Getopt::Std". Declaring the variable with "our" solves the problem, regardless whether "use strict" remains present or not.

      Abigail