DOES() allows you to test for allomorphism, where objects perform similar duties despite different lineages. isa() allows you to test for inheritance, where objects perform similar duties due to a common lineage.

What's allomorphism? You've probably encountered it already in Perl 5. Consider, for example, a normal hash reference, a blessed hash, a tied hash reference, and a blessed scalar with hash dereferencing overloading. You have a Perl function that receives as an argument an entity which is one of those four items. You want to access a key and value within that entity as if it were a hash reference--but you don't know precisely what it is.

To see if you can dereference the reference safely, you have a couple of options. You could use ref to see if the entity is just a hash reference, but that only works for the first one. You could use UNIVERSAL::isa() to check if the entity seems to be a built-in hash (but that definitely fails for the last item and it's really bad style). You could use reftype() from Scalar::Util, but that definitely won't catch the final item.

Besides that, checking the type is the wrong question--you just want to know if you can dereference the entity as a hash. The real question is "does this entity behave as I expect a hash to behave", not "how does this entity achieve its behavior"?

In a similar way when dealing with objects (and, in Perl 6, built-in data types which act as objects), DOES() asks "Does this object or class perform this role? I don't care how it does it. I just want to know if it does."

See Roles: Composable Units of Object Behavior for more information.

Update: Made the example clearer as an example.


In reply to Re: I don't understand UNIVERSAL::DOES() by chromatic
in thread I don't understand UNIVERSAL::DOES() by rlb3

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.