Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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:

  • call can() to see if the object can FETCH() -- works on tied hashes, but also works on tied scalars. Doesn't work so well on regular hashes.
  • force all tied hashes that could ever possibly be written to inherit from HASH -- dictates implementation
  • check that $args isa() HASH or isa() Tie::Hash -- leaves out hashes that don't inherit from Tie::Hash
  • check for the existence of a tied-hash specific method, such as TIEHASH() -- better, but still requires another check that it is a regular hash

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":



  • 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 pondering the Monastery: (7)
As of 2024-04-19 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found