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

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

by LanX (Chancellor)
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!

Replies are listed 'Best First'.
Re^3: OOP: ->Coderef for calling Private Methods (possibilites?)
by tye (Sage) 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

      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        

      Thank you that's helpful! 8 )

      Do you agree that it could be more documented?

      Cheers Rolf

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2017-07-25 22:56 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (383 votes). Check out past polls.