in reply to Need Help: Capture STDOUT without Redirecting

I haven't used it, but the Cookbook recommends IO::Tee which allows you to create a scalar file handle. When you print to it, the output goes to as many handles as you like. This is explained recipe 7.8.

Phil

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

Replies are listed 'Best First'.
Re^2: Need Help: Capture STDOUT without Redirecting
by mnj200g (Novice) on May 04, 2006 at 20:20 UTC
    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

      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.

      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.

Re^2: Need Help: Capture STDOUT without Redirecting
by mnj200g (Novice) on May 05, 2006 at 18:06 UTC
    I am stuck on not using IO:Tee. In my environment, it is considered unvalidated code. All code, including my test scripts, must go through a detailed review and documentation. Any code that I use must go through the same vigorious process. If I use IO:Tee, it will have to go through the same process. I think this would be more problem than it's worth at the moment.

    Before test scripts are executed, I do start UNIX's "script" to capture the log. From my limited experience with "script", it seems that the log file does not get written to until the "script" performs an "exit". I suppose I could call "script" inside of a perl script. Would I have problems identifying which session is the original session and which is the "script" session?

    MNJ