[mplspm]: Params::Validate question
Dave Rolsky
autarch at urth.org
Mon Oct 14 14:14:43 CDT 2002
On Mon, 14 Oct 2002, Brian Parker wrote:
> I would like to subclass Params::Validate so that the default option for
> the 'on_fail' "Global" option is something like this:
>
> 'on_fail' => sub { require IllegalArgumentException;
> IllegalArgumentException->throw(message=>$_[0]); }
>
> ... where 'IllegalArgumentException' is a subclass of
> 'Class::Exception'. I can see from the docs that the recommended way to
> do this is to call 'Params::Validate::validation_options' from each
> module that 'uses' Params::Validate, but I rather make the change in one
> place (we have over 100 .pm files). I suppose I could hack
> Params::Validate to change the default, but I don't like doing that
> either. Can anybody recommend a better way?
Since Params::Validate isn't OO (and be glad, cause you want this
particular module to be fast), you can't subclass it.
You could, however, create your own little wrapper, something like this:
package My::Params::Validate;
require Params::Validate;
sub import
{
my $class = shift;
my $caller = caller;
{
eval "package $caller;";
Params::Validate::validation_options( ... );
}
unshift @_, $caller;
goto &Params::Validate::import;
}
I think that'd work (I just wrote it in this email). That's some serious
Perl voodoo, but I think it should do the trick ;)
If it works (or you get it to work with tweaks), let me know, because it
might be worth documenting as the "official" way to customize P::V's
behavior.
-dave
/*==================
www.urth.org
we await the New Sun
==================*/
--------------------------------------------------
Minneapolis Perl Mongers mailing list
To unsubscribe, send mail to majordomo at pm.org
with "unsubscribe mpls" in the body of the message.
More information about the Mpls-pm
mailing list