Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Well, the problem is that there is a difference between:
$x->method(...)
and
class::method($x, ...)
even if "class" is the class in which $x is blessed, or one of the ancestors of that class. When you say $x->method(...), you are invoking what's called "method dispatch", a (breadth-first) search for a method in the class in which the object is blessed and its ancestors. When you say class::method($x, ...) (basically equivalent to $x->class::method(...)), you are foregoing the search and telling perl which class's method to use.

Now, in this example, you are going straight to UNIVERSAL for its can and isa methods, rather than allowing method dispatch to find the can and isa methods in the (nearer than UNIVERSAL) ancestors of your class (or the class itself). Thus, what you're getting is UNIVERSAL::can, rather than Class::MockObject::can (which is what the method dispatch would have found).

The last thing, though, is that perl almost forces you to do this. If you've got an object, and you don't know what it is, then calling $x->can('foo') could be a really bad idea. Well, that's not true... if you know that $x is, in fact, an "object" then you're cool. The problem is: what if you don't know whether $x is an object (a blessed reference), or if it's just a plain, unblessed reference, or not even a reference at all. Then, if it *can* foo, then it'll return true, but if it *cannot* foo, then it might die! Anyway, the UNIVERSAL::can and UNIVERSAL::isa CPAN modules mentioned by itub are pretty cool and basically deal with this, in a way (but they warn if you do it).

------------ :Wq Not an editor command: Wq

In reply to Re: Test::MockObject doesn't fool UNIVERSAL by etcshadow
in thread Test::MockObject doesn't fool UNIVERSAL by water

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-26 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found