Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Private methods

by adrianh (Chancellor)
on Mar 09, 2003 at 23:17 UTC ( #241624=note: print w/ replies, xml ) Need Help??


in reply to Re: Private methods
in thread Private methods

However, this means overriding a private method!

Surely by definition, if you can override a method it isn't private :-)

That's not to say that private methods are useless, but I would want to see a more pertinent example before I could comment on how you are using it.

Where "private" methods win is when you want to hide your implementation decisions from subclasses. If you call your method as $self->_whatever you leave yourself open to having your code broken if a subclass adds its own _whatever method.

You can also break existing code if you add a _something_else method to a later version of your base class when a subclass already has a method of the same name.

Using lexically scoped coderefs or alternate calling strategies means we can code safe in the knowledge that a subclass is not going to change the intended behaviour of our "private" implementation methods.


Comment on Re^2: Private methods
Select or Download Code
Re: Re^2: Private methods
by Ovid (Cardinal) on Mar 10, 2003 at 01:08 UTC

    Surely by definition, if you can override a method it isn't private :-)

    Heh :) Perl is less modest about its private parts. When "private" methods are merely a matter of convention, scalability suffers. You bring this up quite nicely with you _something_else example. If Perl's OO was cleaner, fewer programmers would struggle with the subtle bugs of accidentally overriding private methods. Considered in that light, perhaps all private methods should be implemented via coderefs to ensure that this problem just goes away.

    The more I consider OO in Perl, the more I'm inclined to agree with tye's insistence that inheritance should be avoided (at least in Perl).

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

      The more I consider OO in Perl, the more I'm inclined to agree with tye's insistence that inheritance should be avoided (at least in Perl)

      Well - I wouldn't go quite that far :-) Although inheritance is often overused in designs - regardless of the implementation language.

      As annoying as these issues can be I find I don't get bitten by them very often. The problem is that it can be a complete bugger to track and fix when it does happen.

      There is also the problem that the lack of these abilities, even if they don't impact many real world situations, can be used in those pointless "perl cannot be used for coding in the large" flame fests :-)

      Perl is less modest about its private parts.
      That's a keeper. :)

      Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://241624]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2014-10-30 17:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls