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

Has anyone used Getopt::Auto? I'm keen to get it going but haven't been able to get my test code (taken from the man page) to run the way I think it should.

From the man page:

"Getopt::Auto" is happy for you to use "long" ("--gnu-style"), "short" ("-oldstyle") or even "bare" command names, ("myprogram edit foo.txt", CVS-style) on the condition that you are consistent. Additionally, if you use bare or long style commands, then any short options passed before a command name will be sent into %main::options. For instance, given use Getopt::Auto ( "edit" => "open a file for editing", "export" => "write out the data as an ASCII file" ); "yourprog -vt edit -x foo.txt" will perform the following: $main::options{v} = 1 $main::options{t} = 1 edit("-x", "foo.txt");

My code:

#!/usr/bin/perl use strict; use warnings; # Test bed for Getopt::Auto use Getopt::Auto ( [ "edit" => "open a file for editing", "export" => "write out the data as an ASCII file" ] ); our %options; { print "Dump arguments!!\n"; foreach ( keys %main::options ) { print "$_ : $main::options{$_}\n"; } }

The result:

[alex@foo dev]$ perl -w go.pl -vt edit -x foo.txt Dump arguments!! Use of uninitialized value in subroutine entry at /usr/lib/perl5/site_ +perl/5.8.0/Getopt/Auto.pm line 140. Can't use string ("") as a subroutine ref while "strict refs" in use a +t /usr/lib/perl5/site_perl/5.8.0/Getopt/Auto.pm line 140. END failed--call queue aborted.

--t. alex
Life is short: get busy!

Replies are listed 'Best First'.
Re: Getopt::Auto question
by jasonk (Parson) on Feb 20, 2003 at 23:21 UTC

    Getopt::Auto handles arguments by calling functions with the same name, so if you specify that your program takes an 'edit' option, you need to make sure it has a subroutine named 'edit'.

      Yes, I got that part, but the functionality that I wanted Getopt::Auto to handle was setting $main::options{'v'} to 1 when I passed '-v' as one of the run-time arguments. That part of it doesn't seem to be happening.

      I kind of hate to write my own argument parsing code, but I may just have to bite the bullet and do it. Or peruse Simon's code and see what's happening inside.

      --t. alex
      Life is short: get busy!
Re: Getopt::Auto question
by talexb (Chancellor) on Feb 21, 2003 at 16:07 UTC

    Thanks to all who responded so far .. my latest version of code is

    #!/usr/bin/perl # use strict; use warnings; # Test bed for Getopt::Auto use Getopt::Auto; our $VERSION = 'x'; print __PACKAGE__ . "Dump arguments!!\n"; while ((my ($k, $v) = each %options)) { print "$k $v\n"; } sub edit { print "In Edit routine (@_).\n"; }
    and this produces
    [alex@foo dev]$ perl -w go.pl -vt mainDump arguments!! Use of uninitialized value in string ne at /usr/lib/perl5/site_perl/5. +8.0/Getopt/Auto.pm line 143.
    This is a low priority item but I may write to Simon eventually to find out what's happening.

    --t. alex
    Life is short: get busy!