This is what local is for IMO. I hear a lot of people groan/make fun of local but it's superb for this usage. If you declare local anything and assign to it within a block, it returns to its former global assignment after the block exits. The assignment holds through the entire calling stack within a block.
use IO::File;
sub stdout_func
{
print "Hello world.\n";
}
stdout_func(); # To STDOUT
# To /tmp/foo.out
{
my $fh = IO::File->new(">/tmp/foo.out");
local *STDOUT = $fh;
stdout_func();
}
# Walla -- back to STDOUT
stdout_func(); # To STDOUT
In reply to Re: redirecting function output
by Anonymous Monk
in thread redirecting function output
by hotshot
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |