It was an unclear example.

I hesitate to use this example, knowing that the likely response will be "Tying won't work that way in Perl 6." To forestall those replies, I know that this is the case. I'm also not interested in questions of "Why are you doing this anyway?" This is an example of the shortcomings of relying on inheritance. I can come up with clever hacks to get around all this, but I would rather that I not have to do so.

If you do have a better solution that achives both of my goals (it should be possible to check that things can handle the operations I'm about to perform on them, and this check should not dictate the implementation of these operations), I'm all ears.

Consider a constructor:

sub new { my ($class, $args) = @_; my $self = bless { dbname => $args->{dbhame} }, $class; $self->init( $args->{init} ); return $self; }

If $args doesn't exist, the two dereferences will fail. We can check for that:

sub new { my ($class, $args) = @_; return unless $args; # ...

If $args isn't a reference to a hash, the two dereferences will fail. Here's where it gets tricky:

sub new { my ($class, $args) = @_; return unless $args; return unless UNIVERSAL::isa( $args, 'HASH' ); # ...

That looks fine, and I was happy to use it until I realized it would break if someone passed in a tied hash. Since neither Tie::Hash or Tie::StdHash inherit from HASH, this will fail if I use a tied hash. There are several possibilities:

I much prefer a system that lets me ask one question: am I dealing with something that acts according to this named set of behaviors?


In reply to Re: Re: Class::Interface -- isa() Considered Harmful by chromatic
in thread Class::Interface -- isa() Considered Harmful by chromatic

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.