Hook::Output::File - Redirect STDOUT/STDERR to a file
use Hook::Output::File; { my $hook = Hook::Output::File->redirect( stdout => '/tmp/1.out', stderr => '/tmp/2.out', ); saved(); undef $hook; # restore previous state of streams not_saved(); } sub saved { print STDOUT "..."; # STDOUT output is appended to file print STDERR "..."; # STDERR output is appended to file } sub not_saved { print STDOUT "..."; # STDOUT output goes to STDOUT (not to file) print STDERR "..."; # STDERR output goes to STDERR (not to file) }
Hook::Output::File redirects STDOUT/STDERR to a file.
Hook::Output::File
STDOUT/STDERR
my $hook = Hook::Output::File->redirect( stdout => $stdout_file, # and/or stderr => $stderr_file, );
Installs a file-redirection hook for regular output streams (i.e., STDOUT/STDERR) with lexical scope.
A word of caution: do not intermix the file paths for STDOUT/STDERR output or you will eventually receive unexpected results. The paths may be relative or absolute; if no valid path is provided, an usage help will be printed (because otherwise, the open() call might silently fail to satisfy expectations).
open()
The hook may be uninstalled either explicitly or implicitly; doing it the explicit way requires to unset the hook variable (more concisely, it is a blessed object), whereas the implicit end of the hook will automatically be triggered when leaving the scope the hook was defined in.
{ my $hook = Hook::Output::File->redirect( stdout => '/tmp/1.out', stderr => '/tmp/2.out', ); some_sub(); undef $hook; # explicitly remove hook another_sub(); } ... # hook implicitly removed
Does not work in a forked environment, such as the case with daemons.
perltie
Steven Schubiger <schubiger@cpan.org>
This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
See http://dev.perl.org/licenses/
To install Hook::Output::File, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Hook::Output::File
CPAN shell
perl -MCPAN -e shell install Hook::Output::File
For more information on module installation, please visit the detailed CPAN module installation guide.