I have a sub that wraps around a hash to keep track of some traits for some items, traits which may or may not have a specified value. Example: if(trait('bob','isAdult')){print trait('bob','age')} One obvious way would be to use defined, as you say. It's a matter of encapsulation; I think it looks nicer if the code that uses this sub doesn't have to worry about that distinction. Another obvious way would be to use two different functions, but again, more visible complexity.

On the one hand, encapsulation is certainly often a Good Thing. On the other, the task you appear to be describing seems to be particularly well suited to Perl's hashes, which offer the distinction between exists and defined. Plus, you seem to want to mess with Perl's notion of Truth and Falsehood.

If I may wager a guess, perhaps you are taking concepts from a different programming language and trying to bend Perl to match them? While Perl is indeed a very pliable langauge and There Is More Than One Way To Do It, perhaps it would make sense to look at what Perl offers natively and implement what you're trying to implement in a perlish way first? For example, next to plain hashes, there are tied hashes, and objects can be overloaded to allow hash dereferencing of an object to return a reference to a hash, including a tied hash. That would allow users of your API to use the Perl hash concepts they are familiar with, while allowing you to keep control over what goes in and out of the hash.

Or, if you want to follow through with encapsulation idea, then it'd be best if you created classes and offered methods such as $bob->traits->get('age'), $bob->traits->is_set('isAdult'), and $bob->traits->set('foo','bar') (see e.g. the API of Hash::Ordered), because at the moment, it sounds to me like you're trying to overload your API a little too much.


In reply to Re^3: "" but true by haukex
in thread "" but true by Chuma

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.