[mplspm]: Params::Validate question

Brian Parker bparker at pobox.com
Mon Oct 14 15:03:07 CDT 2002


Hey Dave,

Here is my first shot:

$ perl -w test.pl
Goto undefined subroutine &Params::Validate::import at
z:/wcBuildTools/BusDataAc
cess/src/lib/WC/ParamsValidate.pm line 19.
BEGIN failed--compilation aborted at test.pl line 3.

I think this is because 'import' exists in the 'Exporter' package.  I'll
take a closer look at this later.  I can see the "jist" of the strategy
you propose.  I'll let you know when I get something to work.

regards,
Brian

Here is my test module:

use strict;
package WC::ParamsValidate;
require Params::Validate;

sub import
{
   my $class = shift;
   my $caller = caller;

   {
      eval "package $caller;";
      Params::Validate::validation_options(
         on_fail => sub { require WC::Exception;
            WC::Exception->throw(message=>$_[0]); }
         );
   }

   unshift @_, $caller;
   goto &Params::Validate::import;   
}

1;

Dave Rolsky wrote:
> 
> 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.


--------------------------------------------------
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