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

Greetings,

I'd like to use the Getopt::Long module at two places: 1) in a generic module providing various general purpose tasks and 2) in a script doing some specific task. This script is of course using the module mentioned under 1). So we have:

The script (I called it mgo.pl for Multiple GetOpt):

#!/usr/bin/perl -w use MGO; use Getopt::Long; GetOptions('help' => \ (my $help), 'version' => \ (my $version), ); print "HELP : $help\n" if(defined $help); print "VERS.: $version\n" if(defined $version);
The module (MGO.pm):
package MGO; use Getopt::Long; GetOptions('cpu=i' => \ (my $cpu), 'quiet' => \ (my $quiet), ); print "CPU : $cpu\n" if(defined $cpu); print "QUIET: $quiet\n" if(defined $quiet); 1;

Now no matter what I do, I get for every option, that is not defined in MGO.pm an "unknown option: <option>". How do I tell GetOpt::Long to ignore unknown options (the docs are silent about this)? Or how do I split the Options processing in the above meantioned way?

Thanks for any hints.

Bye
 PetaMem
    All Perl:   MT, NLP, NLU

Replies are listed 'Best First'.
Re: Multiple GetOptions?
by jeffa (Bishop) on Feb 15, 2004 at 17:25 UTC

    Personally, i see a design problem here ... but i don't know exactly what you are building, either. ;)

    Generally speaking, i think that a module should have an interface that is not dependent on something like Getopt::Long. I feel that Option modules are better fitted with CLI (command line interface) scripts. What if you wanted to used the module from a CGI script or mod_perl -- AKA, a Web interface? If so, then your module should not be pulling arguments from STDIN, instead the client should read from STDIN (or the socket) and simply pass the data as method arguments to the module. It's not really that much more work, but the flexibility outweighs the extra typing, IMHO.

    UPDATE:
    Sounds valid and interesting. Please let me know how it goes. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      The module shall provide explicitedly a "scripting environment". There's a bunch of scripts for natural language processing (esp. statistical NLP) we're using.

      Many of these scripts are standalone, but there is much redundancy. This is a try to provide a "general scripting environment infrastructure" for common tasks and processes of these scripts.

      Bye
       PetaMem
          All Perl:   MT, NLP, NLU

Re: Multiple GetOptions?
by Abigail-II (Bishop) on Feb 15, 2004 at 17:17 UTC
    Getopt::Long will "eat" (that is, remove from @ARGV) options, or what looks like options, until it either encounters --, or it exhausts @ARGV. So, using -- on the command line is an option.

    But you can also use the pass_through configuration option of Getopt::Long. You may also want to play with the prefix_pattern option.

    Abigail

Re: Multiple GetOptions?
by PetaMem (Priest) on Feb 15, 2004 at 17:14 UTC
    It is possible to configure Getopt::Long to achieve the requested behaviour.

    I was simply blind when reading the docs plus was sent the wrong way with the advice to try the "permute" configuration switch.

    It is the pass_through (default: disabled) switch that does the trick!

    Bye
     PetaMem
        All Perl:   MT, NLP, NLU

      Hi,

      It does the trick, apart from the fact that it stops reporting "unknown option" for nonexistent options.
      If you want to have the warning back for unrecognised options you can add the Getopt::Long::Configure qw(nopass_through); where you catch options for the last time.

      -sheep

      Update: Corrected a typo. Thanks to BazB.