meraxes has asked for the wisdom of the Perl Monks concerning the following question:
Esteemed monks, I'm hoping someone can help me.
I'm working on an app that uses Params::Validate in a number of places. Quite a few really. What I want to do is add a method in the base class that uses Contextual::Return. Herein lies the problem:
use Params::Validate qw(validate_pos validate SCALAR ARRAYREF HASHREF) +; use Contextual::Return;
BOOM! It took me a while to realize the problem here. Params::Validate is importing SCALAR, ARRAYREF and HASHREF tags into the current namespace.... and oh look, Contextual::Return uses those as well! ARG!
Now, I know what I can use the full package name with Params::Validate to get the various "type" validators and not import them like so:
use Params::Validate qw( validate_pos validate ); use Contextual::Return; # and later in the args to validate I could do: sub fun { my $self = shift; my $args = validate( @_, { fun => Params::Validate::ARRAYREF } ); return LIST { @{ $args{fun} } } ARRAYREF { $args{fun} } SCALAR { $#{$args{fun}} } DEFAULT { croak "illegal context" } }
but I'd have to go through all the modules and fix all the use statements and expand out the type checks to Params::Validate::ARRAYREF/SCALAR/HASHREF. I might even be able to automate something to do it for me... but that scares me. It's all over the place!
Now, the easy thing would be to do the same with Contextual::Return but I realized my Perl-fu is not as strong as TheDamian's (stunning realization, I know). It doesn't play well like that and from what I can see in the source (yes, you may laugh now at a very average Perl hacker trying to understand Damian's code) there's a whole lot going on that makes just specifying out the full package name a non-option (I've tried). So that means this doesn't work:
use Params::Validate qw( validate_pos validate SCALAR ARRAYREF HASHREF + ); use Contextual::Return qw(); # later, in a subroutine # this don't work! sub funner { return Contextual::Return::LIST { qw/Larry Curly Moe/ } Contextual::Return::ARRAYREF { [ qw/Larry Curly Moe/ ] } Contextual::Return::DEFAULT { croak "listy type contexts only +" } ; }
So, I HAVE a solution, but it seems a bit of a pain to have to go through ALL the code and change every use of Params::Validate in bunches of files so I can use Contextual::Return in one place. Can anyone help me be lazy? I really don't know how else I can stop these two modules from clashing.
Well... maybe I'll ask Rolsky and Damian to arm wrestle for the namespace. :D
Thanks for the assistance.
Updatery: fixed wrong module name in code
Updatery the Second: fixed missing bracket.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: lazily getting around an Exporter problem (Want.pm)
by lodin (Hermit) on Oct 04, 2007 at 22:03 UTC | |
Re: lazily getting around an Exporter problem
by runrig (Abbot) on Oct 04, 2007 at 22:27 UTC | |
Re: lazily getting around an Exporter problem
by chromatic (Archbishop) on Oct 04, 2007 at 20:37 UTC | |
by meraxes (Friar) on Oct 04, 2007 at 21:37 UTC | |
Re: lazily getting around an Exporter problem
by throop (Chaplain) on Oct 04, 2007 at 21:07 UTC | |
by meraxes (Friar) on Oct 04, 2007 at 22:00 UTC |