Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Private Methods Meditation

by dragonchild (Archbishop)
on Jul 19, 2004 at 12:15 UTC ( [id://375529]=note: print w/replies, xml ) Need Help??


in reply to Private Methods Meditation

Here's the question I ask - why do private methods fail in Perl's implementation of them? It's because of the following situation:
package Foo; sub new { bless {}, shift } sub _method1 { print "Foo::_method\n"; } sub method2 { my $self = shift; $self->_method1 } package Foo::Bar; @ISA = qw( Foo ); sub _method1 { print "Foo::Bar::_method\n"; } package main; my $foobar = Foo::Bar->new; $foobar->method2;

The code is being called from within the Foo class, but method lookup finds the _method1() within the Foo::Bar class. *shrugs* I'm not sure where this leads, but that's the reason why. :-)

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

I shouldn't have to say this, but any code, unless otherwise stated, is untested

Replies are listed 'Best First'.
Re^2: Private Methods Meditation
by guha (Priest) on Jul 19, 2004 at 12:27 UTC

    Perhaps just

    sub method2 { my $self = shift; _method1( $self ); }
    or am I missing something really obvious in this example ?

    Or for better general safety, mark the privates to make their names unique.

    package Foo; sub _F_method1 { print "Foo::_method\n"; } sub method2 { my $self = shift; $self->_F_method1; } package Foo::Bar; @ISA = qw( Foo ); sub _F_B_method1 { print "Foo::Bar::_method\n"; } package main; my $foobar = Foo::Bar->new; $foobar->method2;

    update: s/exclusive/unique/

      sub method2 { my $self = shift; _method1( $self ); }

      or am I missing something really obvious in this example ?

      Yes. If you don't override _method1 in the subclass, nothing will be called.

      ----
      send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Re^2: Private Methods Meditation
by tilly (Archbishop) on Jul 19, 2004 at 16:04 UTC
    If you're inheriting from someone else, you're mucking around with their internals.

    If you're uncomfortable doing so, then you shouldn't be inheriting.

      tilly, you have a tendency to toss out statements like that without explaining what you mean (that's not intended as criticism.) Are you stating that all OO languages are like this? Clearly you are referring to more than just a blessed referent. With much tighter encapsulation available with Java or C++, do you mean even these languages have the programmer automatically mucking with internals upon subclassing or is this a peculiarity of Perl (or dynamically-typed languages)?

      Cheers,
      Ovid

      New address of my CGI Course.

        I was stating a general rule for all OO languages. There may be additional traps in Perl, but it is true in general.

        When I declare a subclass, I'm saying that A isa B. Making this work generally requires that A and B cooperate to some extent. At a minimum, the author of A needs to know what methods B is likely to expect to see overridden. Missing some of them may mean that your object behaves weirdly. Sometimes you need access to class data. In many languages the subclass implementor has access to special protected functions, and needs to know when they are supposed to be called. All of this may need updating if the parent module changes. And so on.

        In short, the concept of "isa" represents reasonably tight coupling between components. Some of the tightness is accidental - such as the result of the visibility of the implementation in Perl. But some is intrinsic to what OO is. If you're unwilling to accept this coupling, then you probably shouldn't be subclassing.

        Remember that OO provides a series of potential compromises between reducing duplication of information and reducing coupling. It is good to not duplicate information. It is good to have loose coupling. Those goals are good because each avoids a difficult problem. Unfortunately they naturally conflict, reducing duplicate information naturally couples things together.

        Among those potential compromises, inheritance is the choice that gives the best opportunity for removing duplication of information. But at a natural cost of tighter coupling.

        Can't speak for tilly, but it's defaintely something that effects pretty much any language, though Perl might be worse off than most. If you have a method in your subclass, you always run the risk that the parent class will define a method with the same name, but with very different functionality.

        I don't remember how the method is searched for in Java, but I believe that if you call that method in the parent, then it calls the parent class's version. This isn't true in Perl without some tricks.

        ----
        send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Re^2: Private Methods Meditation
by ihb (Deacon) on Jul 20, 2004 at 00:39 UTC

    I too am bother with this and I shrug when I see     $self->_private(@args) because hardly ever has the author documented &_private so I have to be extra careful when extending it and writing my own help routines.

    And I really cry when I see     $self->_init(@_); in the parent class after trying to debug a totally innocent subclass, which happened to have a little help routine called &_init.

    IMHO, it's just not particularly clever to call a "private" method as a method since you don't need the method dispatch to find the subroutine, plus you risk being accidently overridden.

    Private methods should be invoked as (help) functions, because that's what they are.     _private($self => @args) is my preferred style.

    ihb

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-24 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found