... could you perhaps share the situation you're in where it would be handy for these two to behave differently?

Firstly, I see what you mean by the expectation that print $blessed_obj; and print "$blessed_obj"; should do the same thing. (A nicely insightful observation :-)

My question came about this way:
In C, users of the mpfr library will output values to stdout using the mpfr_out_str function - which prints directly to stdout (rather than return a string which one can then print to stdout). As regards Math::MPFR (which wraps the mpfr library function), I thought "Wouldn't it be nice if print was overloaded to use mpfr_out_str ... then C and Perl are guaranteed of producing the same output." But then, that would mean that every time someone did $x = "$blessed_obj"; the value encapsulated in $blessed_obj is going to be printed to stdout ... and that's probably not what's intended :-)

Anyway, as you've just demonstrated, there's a very good reason that print and "" should be overloaded by the same function ... so I think that's the end of that train of thought. Probably better to have print and "" overloaded by a function that returns a string that emulates the mpfr_out_str output - as is currently the case. Only problem there is that, since the mpfr library itself doesn't provide such a function, I have to hand craft my own ... and therein lies the potential for discrepancies.

Thanks jbert.

Cheers,
Rob

In reply to Re^2: Overloading print() by syphilis
in thread Overloading print() by syphilis

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.