Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: OOP: ->Coderef for calling Private Methods

by LanX (Canon)
on Dec 13, 2008 at 18:30 UTC ( #730180=note: print w/ replies, xml ) Need Help??


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

> I do not see any reason for the difference.

I agree there is no logical reason for extra behaviour...

> And the docs are clear and complete here as it seems to me.

Sorry, I disagree! Till now, I couldn't find any other perldoc describing this feature , and perlop is just a condensed quick reference. IMHO Perl has to many special cases to rely on just 3 words in a quickref.

I think it's quite unknown, taking into consideration that most hacker prefere the _underscore_private_method() convention instead.

Even Merlyn's Book on Perl-Objects and Refrences doesn't list this behaviour, (at least it's not indexed)

For me it looks like the documentation should be improved!

Cheers Rolf

UPDATE: And what really makes my brain stumble, what is the expected behaviour when i call $obj->$code_ref() and code_ref doesn't point to a method (of the blessed package).

Am I supposed to run experiments to find it out?

Please can anyone give me a link to any documentation!


Comment on Re^2: OOP: ->Coderef for calling Private Methods
Select or Download Code
Re^3: OOP: ->Coderef for calling Private Methods (possibilites?)
by tye (Cardinal) on Dec 13, 2008 at 19:42 UTC

    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        

      > $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.

        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.

Re^3: OOP: ->Coderef for calling Private Methods
by ccn (Vicar) on Dec 13, 2008 at 19:48 UTC

    Perl 5.6 does not have that feature (perlop 5.6). That explains why hackers use underscores and merlyn did not mention it in his book.

    Perl doesn't care about $code_ref having to be a reference to a method. It's enough to be just a sub ref. The sub gets an $obj as first argument, that's all.

    Update: broken link fixed

      Thank you that's helpful! 8 )

      Do you agree that it could be more documented?

      Cheers Rolf

      Perl 5.6 does not have that feature

      Testing 5.005_03 shows that even it had the feature of $obj->$code_ref( ... ), despite the 5.6 documentation not mentioning it:

      $ perl5.00503 -e '$cr= sub { warn "( @_ )\n" }; "main"->$cr( "bar" )' ( main bar )

      - tye        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (14)
As of 2014-10-23 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls