Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Question about UNIVERSAL::can

by Arunbear (Prior)
on May 29, 2013 at 16:33 UTC ( [id://1035881]=note: print w/replies, xml ) Need Help??


in reply to Question about UNIVERSAL::can

A package does not have to be loaded to be useful e.g. we can add a sub to a package and then call it, even though we didn't load it:
% perl -E '*Some::Module::foo = sub { say "hello" }; Some::Module::foo +()' hello
thus it makes sense to be able to test it with can (whether we loaded it or not).

It seems that as an empty package can do nothing, searching for 'can' leads to nothing.

% perl -E 'say Some::Module->can("can")'
However this is not the case for a non-empty package:

% perl -E '*Some::Module::foo = sub { }; say Some::Module->can("can")' + CODE(0x259d370)
or even
% perl -E '*Some::Module::foo = sub { say "far out man!" }; say Some:: +Module->can("can")->("Some::Module", "foo")->()' far out man! 1

Replies are listed 'Best First'.
Re^2: Question about UNIVERSAL::can
by schetchik (Beadle) on May 29, 2013 at 21:18 UTC

    Thank you for interesting examples.

    It's another interesting result, that follow from tobyink's link

    $ perl -E'package Foo::Bar; say Foo->can("can")' CODE(0x1f4a220)
    I think, perl just show to symbol table and if data about package exists, that means package exists too, so perl starts inheritance. Otherwise it uses UIVERSAL without inheritance.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found