in reply to Re: Need Help: Capture STDOUT without Redirecting
in thread Need Help: Capture STDOUT without Redirecting

IO:Tee does seem interesting.

However, I cannot modify the sub whose outputs I want. Also, I don't have the module and doubt that the admin would install it.

MNJ

  • Comment on Re^2: Need Help: Capture STDOUT without Redirecting

Replies are listed 'Best First'.
Re^3: Need Help: Capture STDOUT without Redirecting
by ikegami (Patriarch) on May 04, 2006 at 20:50 UTC
    However, I cannot modify the sub whose outputs I want.

    Not a problem.

    #!/usr/bin/perl use strict; use warnings; use IO::Tee (); my $buf; { open(my $buf_fh, '>', \$buf ) or die "Unable to create a write-to-memory handle: $!\n"); my $tee = IO::Tee->new(\*STDOUT, $buf_fh); my $old_select = select($tee); mysub(); select($old_select); } mysub(); } print "buffer: $buf\n"; sub mysub{ print "mysub output\n"; }

    The above is untested. If it doesn't work, I'm sure it's fixable.

    Update: Tested (as much as I can with Perl v5.6.1) and fixed. Keep in mind the previously discussed caveats to using select.

    I don't have the module and doubt that the admin would install it.

    Then install it in your account. This is super easy in this case since the module consists entirely of one .pm.

Re^3: Need Help: Capture STDOUT without Redirecting
by Fletch (Bishop) on May 04, 2006 at 20:49 UTC

    IO::Tee is pure Perl code. If you can copy a file onto the same machine you're running your code (and if you can't, one wonders how you're getting your code there . . .) you can "install" your own copy. Make an IO directory somewhere, put the IO::Tee source in Tee.pm, point perl at the directory containing IO via either PERL5LIB, a command line -I switch, or the standard lib module.