Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: OOP: ->Coderef for calling Private Methods

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


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

> what is the difference to &$secret_door() except that $self is passed as first parameter?

I do not see any reason for the difference. And the docs are clear and complete here as it seems to me.
$obj->$coderef() must behave the same way as $obj->methodname().


Comment on Re: OOP: ->Coderef for calling Private Methods
Select or Download Code
Re^2: OOP: ->Coderef for calling Private Methods
by LanX (Canon) on Dec 13, 2008 at 18:30 UTC
    > 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!

      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 ?

      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://730177]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2014-08-29 21:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls