Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Private methods are completely invisible outside of a class, so they cannot participate in the public interface. Submethods can be called from outside a class just like ordinary methods. The only way that submethods are different from methods is that the dispatcher ignores them unless there's an exact match on the class (either because that's the class of the object in question, or because the class was requested as part of, say, a SUPER::.)

For instance, if you write a constructor such that it can be inherited, then you should declare it a method. But if there are implementation dependencies in your constructor that would make it a bad idea to inherit, then you should declare it a submethod. It's not a private method--you can still call it from outside the class. But any class deriving from your class had better arrange for its own constructor, unless some other class in the tree provides an inheritable constructor.

In particular, initialization routines that work on your particular set of attributes should submethods. It makes no sense to virtualize such a utility routine, because if you do hierarchical construction and destruction (as Perl 6 will), you'd just end up initializing the same attributes more than once if someone else uses your routine on their class that derives from your class. On the other hand, the routine needs to be callable from outside the class, or your child classes can't walk up the class tree to do hierarchical initialization.

On the gripping hand, classes with a standardized list of attributes can inherit a standard initialization routine that is metadata driven, and hence class independent. But it's probably slower than if you hard-wired it for your class. Submethods allow both implementation inheritance and implementation hiding under the same public interface.


In reply to Re: Re^6: Private method variations by TimToady
in thread Private method variations by adrianh

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 20:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found