Welcome to the Monastery | |
PerlMonks |
Method parameters validation and inheritanceby roman (Monk) |
on Sep 18, 2008 at 10:14 UTC ( [id://712220]=perlquestion: print w/replies, xml ) | Need Help?? |
roman has asked for the wisdom of the Perl Monks concerning the following question: Dear monks, this is not a serious problem, but I come across its variants quite frequently. I have a method (say open_for_reading), which accepts hashref of parameters. Since it is quite important method I need all of its parameters to be understood. The open_for_reading method is overriden in a couple of subclasses, each of them understands more parameters.
Without the validation the code is something like:
For parameter validation I found Params::Validate from CPAN. The only way I was able to add the validation is like this:
Instead of one method I have three:
I can live with three methods, but besides open_for_reading there are also open_for_writing, open_for_processing, ... so whole interface suddenly "triplicates". Do you think is there a different solution? Even without the validation there is a "naming" problem of its own. I have an interface method which does initialization and cleanup, and between these two blocks is a code to be overriden in subclasses.
I put the inner block into separate method, but I often have problem to name it. Do you use some naming convention for the "executive" methods? Prefixes (do_, exec_)? Postfixes (_body)?
Back to
Seekers of Perl Wisdom
|
|