Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: UNIVERSAL::can() doesn't do what I need ...

by Vynce (Friar)
on Jun 14, 2001 at 18:18 UTC ( [id://88422]=note: print w/replies, xml ) Need Help??


in reply to UNIVERSAL::can() doesn't do what I need ...

actually, while the method itself might insist on a class name rather than an object, can() won't know that. so $self->can('do_something'); will correctly determine whether or not you can do it.

your situation is made messier if you need to know which of its parents can do it; but that's OK, because you can call ->can() on a string that is the name of a class. Here's a one-liner to prove it.

perl -e 'package It; sub doit{return pop}; $foo="It"; print($foo->can( +"doit")?"y":"n")'

incidentally, i would recommend re-writing the method so that, if given an object rather than a class name, it gets the class name by ref'ing the object. e.g.,

sub class_method { my $proto = shift; $class = ref($proto) || $proto; # do stuff ... }
because there is almost never a reason not to.

.

update (about perldoc UNIVERSAL: yes, I saw that too; and i almost let it stop me from trying. But then i tried it anyway.

and it is OK to doubt me, brother; doubt builds faith, and besides, i am only a man, after all

.

Replies are listed 'Best First'.
Re: Re: UNIVERSAL::can() doesn't do what I need ...
by dragonchild (Archbishop) on Jun 14, 2001 at 18:29 UTC
    I do have the my $class_name = ref($self) || $self; at the top of the function. The thing is that, after the first call to do_something() (which passes an object), I'm solely going to be passing in the class name of the parent. This means that I needed to be able to do a can() on a string, which you say I can.

    Incidentally, the perldoc for UNIVERSAL::can() says that unless the first parameter is a bless'ed reference, it will return undef. I'm not doubting you, but the perldoc then is wrong. :(

Log In?
Username:
Password:

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

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

    No recent polls found