Reputation: 270
I have control over a perl script which is early required from another one which is a CGI script. I need to capture all output of the main script and be able to change it somehow.
This is the actual chaining:
[index.cgi] > (require) > [header.pl] > (eval 'cat ...') > [my_script.pl]
I know I can use the END block to make the post-processing part at the end of the whole program, but I would need a way to start capturing all the output.
EDIT: I have just found Capture::Tiny, which allows us to:
use Capture::Tiny ':all';
($stdout, $stderr, @result) = capture {
# your code here
};
But that would require me to put all the code between curly braces, which is not viable since I don't have access to the main, calling script (which requires my script). This helps to clarify that I want to start capturing the output as soon as I execute the sentence.
Upvotes: 0
Views: 76
Reputation: 386501
Place the following in the file executed by your caller.
#!/bin/sh
/path/to/actual_script "$@" | perl -pe's/.../.../g'
Upvotes: 2
Reputation: 270
This solved my problem:
my $OUTPUT;
my $OUTPUT_FH;
my $OLD_FH;
open( $OUTPUT_FH, '>', \$OUTPUT ) or die;
$OLD_FH = select $OUTPUT_FH;
END {
select $OLD_FH;
close $OUTPUT_FH;
# Change the output
$OUTPUT =~ s/pattern/replacement/flag;
print $OUTPUT;
}
Solution worked from: How to Capture STDOUT in a variable
Upvotes: -1