use strict;
use warnings;
our $exchange = "before (set by caller)";
my $other_file = "this_other_thing.pl";
my ($out, $err) = redir_file( $other_file );
#-- proof of re-direction and import/export
print "Caller: GOT from STDOUT ($other_file): ",
join("\n\tOUT> ", "", split( /\n/, $out) ), "\n";
print "Caller: GOT from STDERR ($other_file): ",
join("\n\tERR> ", "", split( /\n/, $err) ), "\n";
print "Caller: Exchange : $exchange\n";
sub redir_file {
my $do_filename = shift;
#-- we DUP the current STREAMS and then we close the original streams...
open(my $oldout, ">&STDOUT") or die "Can't dup STDOUT: $!";
open(my $olderr, ">&STDERR") or die "Can't dup STDERR: $!";
close STDOUT;
close STDERR;
#-- now, we re-open the streams with redirection into strings...
my $redir_stdout;
my $redir_stderr;
open( STDOUT, '>', \$redir_stdout ) or die "Cannot redirect STDOUT to string: $!";
open( STDERR, '>', \$redir_stderr ) or die "Cannot redirect STDERR to string: $!";
#-- this is like "do"... and exactly as insecure...
my $do_file_content = qx{cat $do_filename}; # e.g. use Slurp...
my $read = eval $do_file_content; # =8-O
#... now $exchange might have been updated
#-- now, we restore the previous streams...
open(STDOUT, ">&", $oldout) or die "Can't dup \$oldout: $!";
open(STDERR, ">&", $olderr) or die "Can't dup \$olderr: $!";
return ($redir_stdout, $redir_stderr);
}
####
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
our $exchange;
print STDOUT "PRG: Hello World!\n";
print STDERR "PRG: Hello Err!\n";
print "PRG: Exchange: ", ( $exchange // '(nothing)'), "\n";
$exchange = "after (called program)";
####
Caller: GOT from STDOUT (this_other_thing.pl):
OUT> PRG: Hello World!
OUT> PRG: Exchange: before (set by caller)
Caller: GOT from STDERR (this_other_thing.pl):
ERR> PRG: Hello Err!
Caller: Exchange : after (called program)