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

Re^5: OOP: ->Coderef for calling Private Methods (possibilites?)

by ccn (Vicar)
on Dec 13, 2008 at 19:57 UTC ( #730192=note: print w/replies, xml ) Need Help??

in reply to Re^4: OOP: ->Coderef for calling Private Methods (possibilites?)
in thread OOP: Obj->Coderef for calling Private Methods

This? perlobj#Method-Invocation
  1. An object is simply a reference that happens to know which class it belongs to.
  2. A class is simply a package that happens to provide methods to deal with object references.
  3. A method is simply a subroutine that expects an object reference (or a package name, for class methods) as the first argument.


Whatever is on the left side of the arrow, whether a reference or a class name, is passed to the method subroutine as its first argument.

  • Comment on Re^5: OOP: ->Coderef for calling Private Methods (possibilites?)

Replies are listed 'Best First'.
Re^6: OOP: ->Coderef for calling Private Methods (possibilites?)
by LanX (Bishop) on Dec 13, 2008 at 20:16 UTC
    Yes THIS could be better. it says:
    How does Perl know which package the subroutine is in? By 
    looking at the left side of the arrow, which must be either
    a package name or a reference to an object, i.e. something 
    that has been blessed to a package. Either way, that's the 
    package where Perl starts looking. If that package has no 
    subroutine with that name, Perl starts looking for it in 
    any base classes of that package, and so on.
    The right side of the arrow typically is the method name, 
    but a simple scalar variable containing either the method 
    name or a subroutine reference can also be used.

    If there was any paragraph saying something like

    $obj->$coderef(@paras) is equivalent to $coderef->($obj,@paras) "no matter if the RHS and the LHS have any package relation"!
    would be a much clearer definiton and avoiding the need to experiment. (well I'm not sure how to express the "no matter" part in clear English, but I hope the point is clear)

    Cheers Rolf

    UPDATED: Typo in code.

      > How does Perl know which package the subroutine is in?

      It concerns calling by name only. If perl has a reference to code it just executes it. There is no need to determine a package for the call. And the sub itself knows the package it belongs to.

        I think it's clear that I talk about misleadingly short docs.

        Documentation has to be clear for any user, even without deeper knowledge of perl internals and the need to run experiments!

        Cheers Rolf

Re^6: OOP: ->Coderef for calling Private Methods (possibilites?)
by ikegami (Pope) on Dec 13, 2008 at 20:03 UTC
    That doesn't describe the RHS of -> at all?

    Nevermind, I see what you're saying.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://730192]
and cookies bake in the oven...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2018-02-21 09:00 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (276 votes). Check out past polls.