I'm working on a project and am trying to settle on some API details. Currently, it is how users will check a value, such as a response code, set by the invocation of a method on an object, where the value is from a small set of possibilities. Actually checking the response code wouldn't need to be done very often unless testing or debugging. But even calls that die will set a response code, so if the die was captured by eval you could interrogate for more information. Etc. So for example, if the object was $client and you had just invoked method foo on it: $client->foo({some => 'args'}); And you wanted to see the specific response code, what might be the most 'natural' syntax? Here are the possibilities I'm considering:

  1. $client->response_code == OK;
  2. $client->response_is(OK);
  3. $client->response_code == $client->OK;
  4. $client->response_is($client->OK);
  5. $client->response_is('OK');

Initially I had a sub-package that exported all the relevant constants, such as OK. However, I have some other message handling methods that would be useful to most of the packages in the set and would just as well make a simple base class. Options 1 & 2 above both require exporting the constants, which doesn't seem to be compatible with use base qw();. Options 3 & 4 work when the class for $client inherits from the messaging base class (which sets the constants internally), but the use of $client twice looks a bit odd to me. Option 5 would remove the need for constants, but it also looks a bit odd to me to be sending a word as a string to check the status - but that could be just be because I'm used to using the constants.

Fellow Monks, what would you prefer to use? Do you have an idea that might be better? And yes, please feel free to include a link to your personal favorite message passing or exception handling module whose interface you like or that you think I should consider using directly to solve my problem. ;)


In reply to OOP style question: Checking a value by spq

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.