Fellow monks!

I have a class with a $obj->method call that returns a value. (Earth shattering, no?) The vast majority of the time, the caller is going to want that value in plain old string format. A small (5%) percentage of calls, they want another, let's say special scalar format. Crucially, sometimes they want both string and special formats from the same call, and calls take around a second, and are non-idempotent. It is possible to convert from special to string after the fact, but not the other way around.

So for my first cut, I had a return object that is initialized with the special representation of a return value, with two methods: $ret->string and $ret->special. Simple enough. But it felt wrong to force the caller to remember to append ->string almost everywhere, so I decided to play around with overload, and was quickly reminded of the pain and potential pitfalls associated with overrides to "just" stringify an object. To wit, one has to overload the following "minimal" set, according to overload:

+ - * / % ** << >> x <=> cmp & | ^ ~ &. |. ^. ~. atan2 cos sin exp log sqrt int "" 0+ bool ~~

I doubt users are going to need trig functions in my case, but everything else is possible, and for the most part, likely.

As an aside, I've always been curious as to why so many overloads are necessary for basic stringification. Could q("") or perhaps even a q(scalar) not have been enough in many cases? Let all of the others automatically derive from that? For example, if my object already stringifies to "apple", why do I also need to define "cmp" to know if $obj lt 'banana' is true? But that's probably a separate question.

So I'd like some other opinions on this design. Having users do $ret->string in 95% of cases is undesirable, but isn't the end of the world.

I'm currently leaning toward returning both, i.e.: my ($string, $special) = $obj->method, (with my $string = $obj->method working in scalar context), but am very much open to other options.

The reason I'm here, though, is to ask: how would you design something like this (i.e., the first paragraph), and why?

As respects performance, an extra subroutine call or two isn't going to matter, as $obj->method is not CPU bound and takes around a second to return.


In reply to OO design: returning string and/or special value by wanna_code_perl

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.