paulj1999
asked on
Redirecting STDOUT to a variable
Hi,
There's a nice perl module which I really want to use:
PDF::Reuse
You create a PDF in the following manner (According to the instructions)
prFile(FILENAME)
...lines of code....
prEnd
if you don't add FILENAME, the output goes to STDOUT. The problem is I need the output in a variable. How can I redirect STDOUT into a variable?
Thanks
There's a nice perl module which I really want to use:
PDF::Reuse
You create a PDF in the following manner (According to the instructions)
prFile(FILENAME)
...lines of code....
prEnd
if you don't add FILENAME, the output goes to STDOUT. The problem is I need the output in a variable. How can I redirect STDOUT into a variable?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
use Tie::Handle::ToMemory;
Commenting on this one.
Sadly it seems quite impossible with this module. Normally, you could use something like IO::Capture::Stdout in this sort of manner:
my $capture = IO::Capture::Stdout->new() ;
$capture->start();
prFile();
...
prDoc( $template );
prEnd();
$capture->end();
my @lines = $capture->read();
my $pdfData = join('', @lines);
But the way PDF::Reuse opens STDOUT is the open() 'trick' open( FILEHANDLE, ">-"); which seems to mean it opens its own handle to the STDOUT, bypassing perl's handle, so it can't be captured. I'd be happily corrected, but this seems to be the case.
We tried many different methods for capturing STDOUT to variables including:
tie *STDOUT, "IO::Scalar", \$variable;
prFile();
...
prEnd();
untie *STDOUT;
(being careful to capture any errors etc. in an eval, otherwise any graceful output of errors to the browser using, STDOUT, is still being redirected to the variable!)
anyways we couldn't get it working, so our solution was to write the PDF::Reuse PDF to a temporary file, open & slurp the contents, then unlink the temporary file, which is a whole lot of extra work for something that should be simple. I'd love it if you could pass a scalar variable to prFile, or your own FILEHANDLE (which could be a properly captured STDOUT), or even have the generated PDF returned as a string on prEnd. That'd be nice, but that's life. Might email the maintainers when I get a chance.
But good luck anyway.
Sadly it seems quite impossible with this module. Normally, you could use something like IO::Capture::Stdout in this sort of manner:
my $capture = IO::Capture::Stdout->new()
$capture->start();
prFile();
...
prDoc( $template );
prEnd();
$capture->end();
my @lines = $capture->read();
my $pdfData = join('', @lines);
But the way PDF::Reuse opens STDOUT is the open() 'trick' open( FILEHANDLE, ">-"); which seems to mean it opens its own handle to the STDOUT, bypassing perl's handle, so it can't be captured. I'd be happily corrected, but this seems to be the case.
We tried many different methods for capturing STDOUT to variables including:
tie *STDOUT, "IO::Scalar", \$variable;
prFile();
...
prEnd();
untie *STDOUT;
(being careful to capture any errors etc. in an eval, otherwise any graceful output of errors to the browser using, STDOUT, is still being redirected to the variable!)
anyways we couldn't get it working, so our solution was to write the PDF::Reuse PDF to a temporary file, open & slurp the contents, then unlink the temporary file, which is a whole lot of extra work for something that should be simple. I'd love it if you could pass a scalar variable to prFile, or your own FILEHANDLE (which could be a properly captured STDOUT), or even have the generated PDF returned as a string on prEnd. That'd be nice, but that's life. Might email the maintainers when I get a chance.
But good luck anyway.