http://www.perlmonks.org?node_id=1069473

fil309 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, How can I redirect a subroutine output to a file without changing the subroutine? Thanks

Replies are listed 'Best First'.
Re: Redirect subroutine output
by syphilis (Archbishop) on Jan 06, 2014 at 08:54 UTC
    Not sure if this counts as a "redirection to file":
    use strict; use warnings; open WR, '>', 'file.out' or die "Can't open WR for writing: $!"; print WR foo(); close WR or die "Can't close WR: $!"; sub foo { return 'FOO'; }
    Cheers,
    Rob
Re: Redirect subroutine output
by Anonymous Monk on Jan 06, 2014 at 09:16 UTC
    write a wrapper-sub of same name and use select to set the default filehandle for output.