in reply to Output redirection using “require” in perl
I would recommend Capture::Tiny.
$ cat b.pl use warnings; use strict; print "Hello, I'm b.pl"; $ cat a.pl use warnings; use strict; use Capture::Tiny qw/capture_stdout/; my $stdout = capture_stdout { require 'b.pl'; }; print "<$stdout>\n"; $ perl -I. a.pl <Hello, I'm b.pl>
Note that using require to execute another Perl file has some caveats: It will search @INC (which as of Perl 5.26 no longer includes the current directory by default), and it will run the file only once, even if you call require multiple times. If that is not desired, I would recommend using do with an absolute pathname instead; make sure to read its documentation on how to check for errors.
|
|---|