Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Question about UNIVERSAL::can

by Arunbear (Parson)
on May 29, 2013 at 16:33 UTC ( #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


Comment on Re: Question about UNIVERSAL::can
Select or Download Code
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
Node Status?
node history
Node Type: note [id://1035881]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (12)
As of 2014-07-22 16:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (119 votes), past polls