Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Object Method Call

by dreadpiratepeter (Priest)
on Jul 26, 2009 at 16:48 UTC ( [id://783350]=note: print w/replies, xml ) Need Help??


in reply to Object Method Call

In addition to what moritz said, you will notice the problem if you attempt to use $self in display. Since you called it as a sub rather than a method $self will be undefined.


-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

Replies are listed 'Best First'.
Re^2: Object Method Call
by AnomalousMonk (Archbishop) on Jul 26, 2009 at 17:39 UTC
    ... you will notice the problem if you attempt to use $self in display.
    However, if the call (from within the  test() subroutine) had been
        display($self);
    there would have been no problem: this would have emulated the effect of the  -> operator and a
        $self->display;
    call.

    This just reinforces moritz and dreadpiratepeter's point that Perl does not know what's in your mind, only in your code.

      How could perl possibly know what's in _my_ mind ... all too frequently, I've absolutely no idea - as I capably demonstrate thro' my rambling postings here :-D

      Sits back and wonders whether the telepathy engine in perl6 will be more than a figment of someones' fevered imagination ;-|

      </silliness>

      A user level that continues to overstate my experience :-))
      Thanks all for the help!

      But there is a slightly difference between display($self) and $self->display(), when a class XY inherits from MyTest and implements it's own display() method. display($self) calls MyTest::display() even when invoked on an instance of XY, whereas $self->display() calls XY::display() when invoked on an instance of XY. So, one could say that display($self) breaks inheritance. This is often bad, but sometimes, it can be a useful trick.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        That usually occurs when you're overriding a method, in which case $self->SUPER::display(); will do the trick.
        So, one could say that  display($self) breaks inheritance.
        It certainly does that if  display is a method.

        This is often bad, but sometimes, it can be a useful trick.
        Hm. Well, I would agree with the word bad, but I think I would delete often unless the goal of the trick is to confuse the heck out of yourself and everyone else. For me, it's hard enough to keep everything in OO programming straight without deliberately trying to trick myself.

        I'm curious: Can you give an example of how this trick might be productively used?

Log In?
Username:
Password:

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

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

    No recent polls found