Help using Getopt::Mixed

S

Stan Brown

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?
 
J

James Willmore

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'

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top