NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.
All the Perl that's Practical to Extract and Report
Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.
A code smell (Score:2)
Thinking about this example, there's a code smell in general with your use of BUILDARGS.
BUILDARGS is really _not_ for munging arguments, it's there to allow for a constructor which doesn't take named argument, like
my $user = User->new($user_id);
If you're using it to add extra arguments, consider using lazy default/builder, per my suggestion. If you're using it to coerce, use a coercion per Stevan's example.
Re: (Score:2)
OK, I'll buy the code smell argument because you have more experience with this than I do, but I don't understand. I like the lazy/default example, but Stevan's example is longer than mine and doesn't seem to buy me anything. Plus, as Stevan pointed out, it has the drawback of an apparently misnamed constructor pair. What am I missing?
Re:A code smell (Score:2)
The advantage of coercion is exactly what Stevan says, it lets you reuse that bit of logic across classes.
The name mismatch is weird, but I'd probably just have people call the constructor like this:
PIPs::QueryParams->new( params => $controller->allowed_query_params );
I honestly don't like _any_ of these options much myself, but I don't understand your APIs or problem domain well enough to come up with a better solution.
There's just something about using BUILDARGS to do what coercion can do that strikes me as weird.
Reply to This
Parent