I would probably try to structure my code so as to avoid the use of $self->SUPER::whatever(). Then I wouldn't need to have a separate method (that subclasses override) to store the spec for Params::Validate.

You can still pull the spec out of the method itself using lexicals and closures.

{ my $spec_sub = sub { { path => 0 } }; sub open_for_reading { validate( @_, $spec_sub->() ); } }

(Note that this is a variation on a suggestion from the Params::Validate author, autarch, from Re: Params::Validate - a powerful tool for robust subroutine design. I use a sub instead of a flat out lexical because of the case where you have a default set to an anonymous reference (e.g., "{}"). In that case, you'll get the same reference every time the method is called—not a new empty one as you might expect. Using an anonymous sub and calling that means you get a fresh new data structure every time.)

Back to the design problem...

If you have a bunch of stuff in open_for_reading, decompose it into private methods that the main open_for_reading then calls. The overridden open_for_reading in the child class can then call the same methods (or some subset of them). They enforce their own parameters, and callers will have to abide by them but won't have to have their own copy of them.

Since we're not talking about real code here, I'm necessarily doing a lot of handwaving. Please let me know if I'm not getting my point across.

As for part two of your question, I'd probably do this:

sub method { my $self = shift; $self->method_init; $self->_method; $self->method_cleanup; }

If you don't like the "_method" naming, you might pick another suffix ("_exec", maybe). I'd prefer a standard set of suffixes over a standard set of prefixes.

Another approach would be to make that group of methods into an object. It can do its initialization during construction, cleanup in DESTROY, and the method itself can keep its name. Then you subclass it and override that one method. I don't think that's always a good idea, but it definitely takes the clutter off somewhere else, and that's often a good thing.


In reply to Re: Method parameters validation and inheritance by kyle
in thread Method parameters validation and inheritance by roman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.