in reply to Re: Re: RFC - Parameter Objects
in thread RFC - Parameter Objects
While this is nice and convenient for those cases where you have independently valid parameters, it's impossible (or dangerous, you choose) to have pairs (or more) of parameters that are valid together :
my $ratio = Sub::ParamObject->new({ nominator => qr/^[-+]?\d+$/, denominator => sub { $_[1] =~ qr/^\d+$/ and $_[1] != 0 }, }); # works and is convenient $ratio->nominator(-1); $ratio->denominator(1); my $source = Sub::ParamObject->new({ selector => qr/^filename|url$/, filename => sub {$_->[0]->{selector} eq 'filename' and $_[1] =~ +qr/^\w+$/ }, url => $_->[0]->{selector} eq 'filename' and $_[1] =~ qr!^h +ttps?://!i }, }); # and now ??? # I want to have selector set to one of the values # and (only) the corresponding field set. $source->selector('filename')->filename('foo.txt'); # works $source->filename('foo.txt')->selector('filename'); # cannot in my i +mplementation
Either you make the approach dependent on the order of parameters (plausible but ugly IMO), or you find another approach to validation (which I would rather welcome, since I did stuff like this years ago and didn't find a good solution to this).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re(4): RFC - Parameter Objects (validation)
by Ovid (Cardinal) on May 14, 2003 at 20:33 UTC | |
by hossman (Prior) on May 14, 2003 at 21:35 UTC | |
|
Re^4: RFC - Parameter Objects (dependencies are problematic)
by Aristotle (Chancellor) on May 21, 2003 at 14:26 UTC |