in reply to Output redirection using “require” in perl

Use select:

$ cat b.pl #!/usr/bin/env perl use strict; use warnings; print "foo!\n"; $ perl -I. -e 'require "b.pl";' foo! $ perl -I. -e 'open my $o, ">out"; select $o; require "b.pl";' $ cat out foo!

Replies are listed 'Best First'.
Re^2: Output redirection using “require” in perl
by bliako (Abbot) on Jul 03, 2018 at 10:24 UTC

    Adding *STDOUT = $o; to the above, will make it succeed in redirecting also the print statements which explicitly state the filehandle to be STDOUT, e.g. print STDOUT "...":

    perl -I. -e 'open my $o, ">out"; *STDOUT = $o; select $o; require "b.pl";'
Re^2: Output redirection using “require” in perl
by haukex (Archbishop) on Jul 03, 2018 at 09:58 UTC

    Sandeep Kumar: Note that using select only redirects default prints and writes, it doesn't redirect, for example, print statements explicitly made to a filehandle (print STDOUT ...) and it doesn't redirect the output of external commands.