I followed the link to Redirecting STDOUT, lexically scoped and tried the following test script (based on an example I found there):
use warnings; use Math::MPFR qw(:mpfr); print "Hello world\n"; { local(*STDOUT); open(STDOUT, ">", "output.txt") or die $!; #line 10 $num = Math::MPFR->new(); #$num is a '@NaN@' Rmpfr_out_str($num, 10, 0, GMP_RNDN); close STDOUT; } print "Goodbye world\n";
Rmpfr_out_str (which wraps the mpfr library's mpfr_out_str function) prints directly to stdout, so I expected that output.txt would contain @NaN@ and the output to the console would look like:
Hello world Goodbye world
However, output.txt is empty, and the console output looks like:
Hello world @NaN@Goodbye world
Apparently, redirecting perl's stdout has had no effect on xs's stdout. I guess I would need to redirect stdout from within the XSub.

There's another issue, too, I believe. I think I really want to redirect the output to a variable (which I can return) rather than to a file. But if I replace output.txt with a variable (say, $output) then the script just errors out with No such file or directory at try.pl line 10.

I'm starting to wonder if this approach (interesting though it is) is going to lead to a practical solution.

Wouldn't I be better off requesting (politely, of course) of the mpfr developers that future releases of the mpfr library provide a function that returns the same formatted string as output by mpfr_out_str ?

Cheers,
Rob

In reply to Re^6: 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.