Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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

by tye (Sage)
on Dec 13, 2008 at 19:42 UTC ( #730186=note: print w/replies, xml ) Need Help??

in reply to Re^2: OOP: ->Coderef for calling Private Methods
in thread OOP: Obj->Coderef for calling Private Methods

What are the possibilities that you can't decide between without experiments? $obj->$code_ref(...) is the same as $code_ref->( $obj, ... ) and that seems pretty clear from the documentation.

That the method is specified from a code ref would only change the fact that there is no need to look anything up in @ISA.

code_ref doesn't point to a method

Perl 5 has no concept of "method" separate from "sub" so it isn't possible for a code ref to point to something that isn't just the same as a method.

Clearly you can use this technique to dispatch to something that can't be used as a regular method of a class since otherwise there would be little point in using the word "private" in the description.

- tye        

Replies are listed 'Best First'.
Re^4: OOP: ->Coderef for calling Private Methods (possibilites?)
by LanX (Bishop) on Dec 13, 2008 at 19:47 UTC
    > $obj->$code_ref(...) is the same as $code_ref->( $obj, ... ) and that seems pretty clear from the documentation.

    References ?

      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.

        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.

        That doesn't describe the RHS of -> at all?

        Nevermind, I see what you're saying.

      The second half of the documentation reads

      the right side is a method name or a simple scalar variable containing either the method name or a subroutine reference, and the left side must be either an object (a blessed reference) or a class name (that is, a package name). See perlobj.

      I don't see how it could be clearer that a reference is allowed.

        "either the method name or a subroutine reference",

        Till now, I haven't run any tests, but it's not clear to read if it's a subroutine reference to a method or just to any subroutine.

        some additional phrases and examples are missing!

        It's perfectly possible that perl might do a check if the coderef is in the STASH of the class.

        and I don't see why such a powerful feature should be hidden in 3 dispersed half-phrases in the perldocs.

        Cheers Rolf

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://730186]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2018-06-20 22:02 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.