QM has asked for the wisdom of the Perl Monks concerning the following question:
[Note that I've modified Declare.pm to capture the eval string in a variable before the eval, for debugging.]#!/your/perl/here use strict; use warnings; use Getopt::Declare; our $options; $options = Getopt::Declare->new( <<'OPTIONS' ); -one <one:i> First parameter { defer { reject ( $one > $two => '\$one ($one) > \$two ($two)' ) } } -two <two:i> Second parameter { defer { reject ( $one > $two => '\$one ($one) > \$two ($two)' ) } } OPTIONS print "one = $options->{-one}\n"; print "two = $options->{-two}\n"; __INVOKED_WITH__ perl declare.pl -one 10 -two 20 __OUTPUT__ Error: in generated parser code: Global symbol "$two" requires explicit package name at (eval 7) line 1 +00. Global symbol "$one" requires explicit package name at (eval 7) line 1 +44.
The problem is that in the reject block $one is created as a my variable inside an eval string. So $two doesn't exist. And vice versa.
Is it possible to do this with Getopt::Declare?
Also, what about default values? I would prefer to preload default values, then have the reject block execute on the final result. However, the returned object doesn't exist to preload before Getopt::Declare->new().
One method is to define a validate sub in main that's called when each parameter is seen, to save the value, and do validation if possible. However, if I'm going to do that, I might as well just do this:
I was wondering if I was missing this functionality in Getopt::Declare. And if it's not there, what would it take to extend Getopt::Declare to take default values and an appropriate post-processing block?sub validate { my $options = shift; $options->{-one} = 1 unless ( exists( $options->{-one} ) ); $options->{-two} = 2 unless ( exists( $options->{-two} ) ); die "Error: -one ($options->{-one}) > -two ($options->{-two}), " if ( $options->{-one} > $options->{-two} ); } $options = Getopt::Declare->new( <<'OPTIONS' ); ... OPTIONS validate( $options );
-QM
--
Quantum Mechanics: The dreams stuff is made of
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getopt::Declare parameter variables
by Anonymous Monk on Aug 25, 2004 at 04:07 UTC | |
by QM (Parson) on Aug 25, 2004 at 17:44 UTC |