Reputation: 862
I am trying to redirect the output from a library's function without changing the code in the library:
program.pl
use Lib::xxLib1xx;
...
xxLib1xx::Function1($Arg1);
xxLib1xx.pm
Function1{
my $arg = shift;
print "$arg\n";
}
How can I modify the code in program.pl so the when I call Function1, no output is seen? I cannot change the code in the Library itself. If I did a system call, it would look like:
system("echo hello > nul");
Upvotes: 2
Views: 732
Reputation: 862
Answered with help from shooper's response:
my $LOG;
open ($LOG, '>>', 'null');
select $LOG;
...
xxLib1xx::Function1($Arg1);
...
select STDOUT;
Upvotes: 0
Reputation: 626
This is answered well at http://perltricks.com/article/45/2013/10/27/How-to-redirect-and-restore-STDOUT
Hope that helps.
Upvotes: -1
Reputation: 4445
And an answer without using CPAN modules can still be pretty compact:
my $stdout;
{
local *STDOUT;
open STDOUT, ">", \$stdout;
xxLib1xx::Function1($Arg1);
}
print "Got '$stdout' from subroutine call!\n";
Upvotes: 4
Reputation: 2320
Check out Capture::Tiny
use Capture::Tiny qw[ capture ];
( $stdout, $stderr, @result) = capture { xxLib1xx::Function1($Arg1) };
Upvotes: 3