Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Re: Calling can method

by gildir (Pilgrim)
on May 30, 2001 at 13:52 UTC ( [id://84198]=note: print w/replies, xml ) Need Help??


in reply to Re: Calling can method
in thread Calling can method

No no no no,

I can call any static method by ClassName->method. Including 'can' method. I can safely call Test->can('foo'). I have tried that. It works and I use it frequently.

Replies are listed 'Best First'.
Re: Re: Re: Calling can method
by merlyn (Sage) on May 30, 2001 at 17:01 UTC
    You can't safely call $x->can('foo') though, although you can always call UNIVERSAL::can($x, 'foo'), which is where the idiom comes from. True, it's overkill for the class case, but for the instance case, it's mandatory.

    -- Randal L. Schwartz, Perl hacker

      Well, I wonder in what circumstances I cannot call $x->can('foo') safely.

      Only scenario that comes to my mind is when author of class that implements object $x overrides method 'can'. But if such an author would do this, he is supposed to know what he is doing, as 'can' is a well known method together with 'isa'.

        When can't you call $x->can('foo') safely? When $x can't can(), of course!
        for $x ('main', undef, \1) { if (UNIVERSAL::can($x, 'can')) { # ok print "can can\n"; $x->can('foo'); print "\n"; } else { # error: Can't call method "can" on undefined value/unblessed refe +rence ... print "can't can\n"; eval { $x->can('foo') }; print "$@\n"; } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-19 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found