Help using Getopt::Mixed

Discussion in 'Perl Misc' started by Stan Brown, Nov 2, 2003.

  1. Stan Brown

    Stan Brown Guest

    I'm trying to add a usage message to a script that uses Getopt::Mixed. I
    want to print this message whenever a bad commnad line option is passed to
    the script. I' using the module like this:

    use Getopt::Mixed "nextOption" ;


    Getopt::Mixed::init('F i e f=s d:i debug:i configfile=s');
    my $badOption = \&print_usage;
    while (($option, $value, $pretty) = nextOption()) {
    if(( $option eq 'f') || ( $option eq 'configfile' ))

    I just added the "print_usage" line, based upon my reading of the perldoc
    page on this module, but it's not calling the print_usage() function
    (which is defiend earlier in the script, when I pass it an undefined
    argument (-Z for instance).

    Instead, I get the following generic error mesage:

    webcam.pl: unrecognized option `-Z'

    What am I doing wrong here?
     
    Stan Brown, Nov 2, 2003
    #1
    1. Advertisements

  2. Try:

    --worked for me, but my not for you--
    #!/usr/bin/perl -w

    use strict;
    use warnings;
    use diagnostics;

    sub print_usage{
    print "Usage .....\n";
    print "Bad option was: $_[1]\n";
    #you _need_ the 'exit' here, or you'll get an infinite loop :)
    exit;
    }

    use Getopt::Mixed qw(nextOption) ;

    Getopt::Mixed::init('F i e f=s d:i debug:i configfile=s');
    $Getopt::Mixed::badOption = \&print_usage;
    while (my($option, $value) = nextOption()) {
    if(( $option eq 'f') || ( $option eq 'configfile' )){
    print "if stuff here ...\n";
    }
    }
    Getopt::Mixed::cleanup();
    ----end---

    I'm thinking the author left out the part about needing the
    '$Getopt::Mixed' from the begining of 'badOption'. 'badOption' is a
    reference to a subroutine that's used in the module. In order to set
    this reference, you need to define where the reference points to
    (which you tried) _ and_ what it's namespace is (which you didn't do,
    but the author did not make clear - the 'Getopt::Mixed' portion).

    Plsu, I don't think you need the $pretty" in the exampl you gave. I'm
    not even sure _where_ you got that from. There's no refereence to it
    in the documentation that I found. But, to be fair, I'd only read
    what I needed to read :)

    I know what I'm trying to say, but not sure if I'm saying it
    correctly. Bottom line - it worked for me and I'm thinking it'll work
    for you :)

    HTH

    --
    Jim

    Copyright notice: all code written by the author in this post is
    released under the GPL. http://www.gnu.org/licenses/gpl.txt
    for more information.

    a fortune quote ...
    Mark's Dental-Chair Discovery: Dentists are incapable of asking
    questions that require a simple yes or no answer.
     
    James Willmore, Nov 2, 2003
    #2
    1. Advertisements

  3. Stan Brown

    Stan Brown Guest

    Thanks, that was exactly what I had wrong. Now it works.

    Thanks, again.
     
    Stan Brown, Nov 2, 2003
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.