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

Monks,

I've been trying the different solutions to redirect STDOUT to a variable (on Windows) so that I can use it later when sending an email. Here's a few of the nodes' that I've read:

  1. Redirecting all output to a var for post processing?
  2. Redirecting STDOUT
And here is my code using IO::Scalar along with the output:
use warnings; use strict; use Mail::Sender; use IO::Scalar; my $output; my $date = '31Jul200'; my $logfile = "d:\\log\\$date.log"; my $elogfile = "d:\\log\\exported\\$date.elog"; tie (*STDOUT, 'IO::Scalar', \$output); # Use this to capture STDOUT my $cmd = `fw logexport -i $logfile -o $elogfile -r 1000 -n`; untie *STDOUT; # Use this to return STDOUT to normal print "my output='$output'"
Produces:
D:\scripts>perl temp.pl Failed to open file 'd:\log\31Jul200.log': The system cannot find the file specified. untie attempted while 1 inner references still exist at temp.pl line 14. Use of uninitialized value in concatenation (.) or string at temp.pl line 15. my output=''
And using IO::Capture::Stdout:
use warnings; use strict; use IO::Capture::Stdout; my $capture = IO::Capture::Stdout->new(); my $date = '31Jul200'; my $logfile = "d:\\log\\$date.log"; my $elogfile = "d:\\log\\exported\\$date.elog"; $capture->start(); my $cmd = `fw logexport -i $logfile -o $elogfile -r 1000 -n`; $capture->stop(); my $line = $capture->read; print "$line";
Produces:
D:\scripts>perl temp.pl Failed to open file 'd:\log\31Jul200.log': The system cannot find the file specified. Error retreaving captured text in IO::Capture::Stdout at temp.pl line 16 Use of uninitialized value in string at temp.pl line 19.
Any ideas what I'm doing wrong?

Replies are listed 'Best First'.
Re: Redirecting STDOUT to Variable - Not Having Much Luck
by Mr. Muskrat (Canon) on Aug 05, 2003 at 16:43 UTC

    You are using backticks which capture program output and storing the result in $cmd. You never print that output therefore $output is empty.

    use warnings; use strict; use IO::Scalar; my $output; tie (*STDOUT, 'IO::Scalar', \$output); # Use this to capture STDOUT print `echo foo`; untie *STDOUT; # Use this to return STDOUT to normal print "my output='$output'" __DATA__ my output='foo '

Re: Redirecting STDOUT to Variable - Not Having Much Luck
by bobn (Chaplain) on Aug 05, 2003 at 17:23 UTC

    This line:

    my $cmd = `fw logexport -i $logfile -o $elogfile -r 1000 -n`;
    should already capture the output of the command, (at least that which goes to stnadard output). So why are you redirecting *STDOUT?

    --Bob Niederman, http://bob-n.com
Re: Redirecting STDOUT to Variable - Not Having Much Luck
by dragonchild (Archbishop) on Aug 05, 2003 at 16:36 UTC
    Try creating the file first with nothing in it.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.