Let's break this down just a bit. First you say you want to ensure that a container can only contain certain types of things. In programming-speak, that says "comparison required". So you have two choices: string or integer comparison. Unless performance is critical, string seems fine to me.

Now, as to how you're testing, by using the inheritance tree to call the right function, that doesn't seem too bad to me, either. Normally one would check the other way around - check if $obj_to_insert->isa($self->contained_type()), I think, but either way could work, depending on the design goals.


In reply to Re: "polymorphism" hack: nuts or wise? by Tanktalus
in thread "polymorphism" hack: nuts or wise? by rvosa

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.