Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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

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.