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

I'm using Getopt::Declare, but I also want to validate parameter values against each other, as in the following:
#!/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.
[Note that I've modified Declare.pm to capture the eval string in a variable before the eval, for debugging.]

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:

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 );
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?

-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
    #!/your/perl/here use strict; use warnings; use Getopt::Declare; unshift @ARGV, -one => 1, -two => 2 # <- defaults unless "@ARGV" =~ /(-help|-version)/; my $options = Getopt::Declare->new(<<'SPEC'); -one <one:i> first param: (default = 1) [repeatable] {$::one = $one; defer{ die "\t -one ($::one) > -two ($::two)\n" if $::one > $::two } } -two <two:i> second param: (default = 2) [repeatable] {$::two = $two} SPEC print "one = $options->{-one}\n"; print "two = $options->{-two}\n";
      Thanks, I knew I didn't have my head on straight!

      There are 2 minor typos in your example, which Getopt::Declare makes easy to find.

      First, there doesn't appear to be a tab between the specifications and the descriptions (but this may be due to the cut and paste).

      Second, a blank line is needed between each parameter spec and the next.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of