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).
perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The
$d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider
($c = $d->accept())->get_request(); $c->send_response( new #in the
HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|