in reply to Re: Overloading print()
in thread Overloading print()

... 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

Replies are listed 'Best First'.
Re^3: Overloading print()
by moritz (Cardinal) on Sep 05, 2007 at 09:46 UTC
    Why don't you make Math::MPFR capture the output to STDOUT by localizing *STDOUT in the wrapper functions and redirecting it to a string?

    Then you can return the strings instead of printing them, and produce all output with perl's print.

    Or do you do that already?

      Or do you do that already?

      Nope ... but it sounds promising. Where do I find the relevant documentation ? (Haven't done anything like this before, afaicr.)

      Cheers,
      Rob
Re^3: Overloading print()
by jbert (Priest) on Sep 05, 2007 at 10:15 UTC
    Cool, thanks for the explanation.

    So...it sounds as though the problem is problematic API. So the *real* fix is to refactor the underlying MPFR "output string" func to call a 'format to string' helper function, which you can then use for your stringification.

    I appreciate that might not be easy/desirable/practical (mimicing the func in perl is probably easier), but hey.

      ... the *real* fix is to refactor the underlying MPFR "output string" func to call a 'format to string' helper function

      At which point syphilis is thankful that it's impossible to panic oneself to death ....

      (Anyway, you're wrong ... the *real* fix is to delegate responsibility :-)))))))

      Cheers,
      Rob
Re^3: Overloading print()
by pilcrow (Sexton) on Sep 05, 2007 at 17:28 UTC
    "Wouldn't it be nice if print was overloaded to use mpfr_out_str" ... 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

    mpfr_get_str() probably does what you want here. If so, I'd wager that Math::MPFR already does what you want, since it exposes Rmpfr_get_str() and, using it, already overloads its objects' stringification.

    -pilcrow
      mpfr_get_str() probably does what you want here

      Unfortunately it doesn't. All it does is return a string of digits (with a leading minus sign if it's a negative number) for the mantissa, and sets a variable to the value of the exponent. It's then up to Rmpfr_get_str to put the number into scientific notation, based on the information provided by mpfr_get_str.

      If, as one would instinctively expect, mpfr_get_str returned a string formatted in the same way as the mpfr_out_str output, then you would be absolutely correct ... and this whole thread would never bave been born :-)

      Cheers,
      Rob