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


In reply to Getopt::Declare parameter variables by QM

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.