You just have no choice, if you don't have an instance of the class. So if there is no object created, the UNIVERSAL::can is the only way to test if that class provides that method.

Cheers,

Jeroen
"We are not alone"(FZ)

Update: Ahum... indeed. The perl OO is really confusing. But, now I remember again, UNIVERSAL::can('Test','foo') is actually the same as Test->can('foo'). Because in the last case, the word Test gets unshifted into @_, and the method 'can' is found in UNIVERSAL. So internally, actually UNIVERSAL::can is called, with the args ('Test', @_), which comes down to the same.

So this is actually a feature, not a bug {grin}

To clarify a bit more (mehopes):

package Test; package main; print "it works\n" if Test->UNIVERSAL::can('can'); print "it works\n" if Test->SUPER::can('can');
You see? The Package::can only gives the route to the method (if not given, perl finds it from ISA). The -> only tells which thing to unshift in @_.

Of course, the sub need a way to filter out whether they are called with a Package or an object. So the trick doesn't work with all methods.

It's explained also in the 'Inheritance' section of perltoot.


In reply to Re: Calling can method by jeroenes
in thread Calling can method by gildir

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.