Magic. This isn't tested code and if it doesn't work right off the bat, it at least demonstrates the idea here. Of course, the proper way to address this is not to print lots of stuff and capture it but instead rethink your organization so this isn't necessary.
use IO::Handle; my $captured = IO::Handle->new; tie *$captured, 'Capture::Handle', handle => $captured or die "Couldn't tie *CAPTURED: $!"; select CAPTURED; print .... my $captured = tied(*CAPTURED)->get_captured; package Capture::STDOUT; sub TIEHANDLE { my $class = shift; bless { @_ }, $class } sub set_handle { shift()->{'handle'} = shift } sub get_handle { shift()->{'handle'} } sub PRINT { my $self = shift; print $self->{'handle'} @_; push @{$self->{'captured'}}, @_; } sub get_captured { @{shift()->{'captured'}} }
In reply to Re: Appending print to a variable
by diotalevi
in thread Appending print to a variable
by eric256
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |