Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I capture Perl's STDOUT in a variable?

Tags:

perl

I am calling a function that writes to STDOUT using print. How can I capture this in a variable?

Note that all this happens within the same process.

like image 590
R.D Avatar asked Aug 18 '10 02:08

R.D


People also ask

How do I capture stderr in Perl?

You can use Capture::Tiny to capture stdout, stderr or both merged. Show activity on this post. I'm personally a fan of the core module IPC::Open3, though the answer with 2>&1 will get both stderr and stdout in the same stream (and usually good enough).

What is STDOUT in Perl?

STDOUT. STDERR.


2 Answers

If the code in question is not using STDOUT explicitly (i.e., it just does print "..."), you can use select to change the filehandle that print uses:

my $output;
open(my $outputFH, '>', \$output) or die; # This shouldn't fail
my $oldFH = select $outputFH;
call_code_that_prints();
select $oldFH;
close $outputFH;

print $output;    # Here's what we collected

Using select makes it easier to restore STDOUT afterward. Closing and reopening STDOUT is harder to undo. Note that select does not affect STDOUT itself, so it doesn't affect external processes, but you said you didn't have any. It also doesn't affect code that does something like print STDOUT "...".

If the select method isn't sufficient for your needs, I'd recommend you try Capture::Tiny. It can capture output from external programs and code that writes to STDOUT explicitly. But it can't (currently) capture only STDOUT; it always captures both STDOUT and STDERR (either separately or merged into one string).

like image 180
cjm Avatar answered Sep 19 '22 05:09

cjm


The new, cool way to handle this is with Capture::Tiny. You can use it surgically to affect just the part of the program where you need it without disturbing anything else. But, I'd probably do what cjm recommends since that doesn't require a module.

like image 23
brian d foy Avatar answered Sep 18 '22 05:09

brian d foy