Click here to get back home

Redirecting STDOUT under ActivePerl on Windows XP

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Redirecting STDOUT under ActivePerl on Windows XP Keith Thompson 06-26-2008
Posted by Keith Thompson on June 26, 2008, 5:33 pm
Please log in for more thread options
I want to redirect STDOUT to a file, and have the redirection apply to
any programs that I invoke. Here's a brief outline of what I'm doing
(full sources to follow):

open SAVE_STDOUT, '>&STDOUT' or die "SAVE_STDOUT: $!\n";
open STDOUT, '>', 'log.txt';
system "some_command";
close STDOUT;
open STDOUT, '>&SAVE_STDOUT' or die "Restoring STDOUT: $!\n";

The invoked command itself invokes another command. I want to collect
the standard output of both commands in a single file. (I also want
to redirect stderr, but I'm leaving that out for now.)

Under Cygwin (Perl 5.8.8) and Linux (Perl 5.8.0), it works fine.

Under Windows XP (ActiveState Perl 5.6.1), the output of the invoked
command is properly redirected to the file, but the output of the
inner command is just lost, though I've confirmed that it is executed.

Any suggestions? (Upgrading ActiveState Perl is not currently an
option, but it would be nice to know if a newer version fixes this.)

I'm using 3 Perl programs, called "outer.pl", "middle.pl", and
"inner.pl" (the ".pl" suffix is needed to make Windows recognize that
these are Perl scripts).

===== outer.pl =====
#!/usr/bin/perl

use strict;
use warnings;

print "Running on $^O\n";

my $prefix;
if ($^O =~ /^mswin/i) {
$prefix = '.\';
}
else {
$prefix = './';
}

print "Begin outer\n";

open SAVE_STDOUT, '>&STDOUT' or die "SAVE_STDOUT: $!\n";
print SAVE_STDOUT if 0; # avoid "used only once" warning

open STDOUT, '>', 'log.txt';

print "outer calling middle\n";
system "$middle.pl";
print "outer after middle\n";

close STDOUT;
open STDOUT, '>&SAVE_STDOUT' or die "Restoring STDOUT: $!\n";

print "End outer\n";

open my $OUTER_LOG, '>', 'outer.log';
print $OUTER_LOG "outer, pid=$$\n";
close $OUTER_LOG;
===== end outer.pl =====

===== middle.pl =====
#!/usr/bin/perl

use strict;
use warnings;

my $prefix;
if ($^O =~ /^mswin/i) {
$prefix = '.\';
}
else {
$prefix = './';
}
print "Begin middle\n";

system "$inner.pl";

print "End middle\n";

open my $MIDDLE_LOG, '>', 'middle.log';
print $MIDDLE_LOG "middle, pid=$$\n";
close $MIDDLE_LOG;
===== end middle.pl =====

===== inner.pl =====
#!/usr/bin/perl

use strict;
use warnings;

print "In inner\n";

open my $INNER_LOG, '>', 'inner.log';
print $INNER_LOG "inner, pid=$$\n";
close $INNER_LOG;
===== end inner.pl =====

On Cygwin or Linux, log.txt contains:
==========
outer calling middle
Begin middle
In inner
End middle
outer after middle
==========

On Windows (executing outer.pl from a CMD window), log.txt contains:
==========
outer calling middle
Begin middle
In inner
End middle
outer after middle
==========

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Posted by Brian Helterlilne on June 26, 2008, 7:53 pm
Please log in for more thread options
Keith Thompson wrote:
> I want to redirect STDOUT to a file, and have the redirection apply to
> any programs that I invoke. Here's a brief outline of what I'm doing
> (full sources to follow):
>
> open SAVE_STDOUT, '>&STDOUT' or die "SAVE_STDOUT: $!\n";
> open STDOUT, '>', 'log.txt';
> system "some_command";
> close STDOUT;
> open STDOUT, '>&SAVE_STDOUT' or die "Restoring STDOUT: $!\n";
>
> The invoked command itself invokes another command. I want to collect
> the standard output of both commands in a single file. (I also want
> to redirect stderr, but I'm leaving that out for now.)
>
> Under Cygwin (Perl 5.8.8) and Linux (Perl 5.8.0), it works fine.
>
> Under Windows XP (ActiveState Perl 5.6.1), the output of the invoked
> command is properly redirected to the file, but the output of the
> inner command is just lost, though I've confirmed that it is executed.
>
> Any suggestions? (Upgrading ActiveState Perl is not currently an
> option, but it would be nice to know if a newer version fixes this.)
>
> I'm using 3 Perl programs, called "outer.pl", "middle.pl", and
> "inner.pl" (the ".pl" suffix is needed to make Windows recognize that
> these are Perl scripts).
>
> ===== outer.pl =====
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> print "Running on $^O\n";
>
> my $prefix;
> if ($^O =~ /^mswin/i) {
> $prefix = '.\';

$prefix = 'perl .\';
> }
> else {
> $prefix = './';
> }
>
> print "Begin outer\n";
>
> open SAVE_STDOUT, '>&STDOUT' or die "SAVE_STDOUT: $!\n";
> print SAVE_STDOUT if 0; # avoid "used only once" warning
>
> open STDOUT, '>', 'log.txt';
>
> print "outer calling middle\n";
> system "$middle.pl";
> print "outer after middle\n";
>
> close STDOUT;
> open STDOUT, '>&SAVE_STDOUT' or die "Restoring STDOUT: $!\n";
>
> print "End outer\n";
>
> open my $OUTER_LOG, '>', 'outer.log';
> print $OUTER_LOG "outer, pid=$$\n";
> close $OUTER_LOG;
> ===== end outer.pl =====
>
> ===== middle.pl =====
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $prefix;
> if ($^O =~ /^mswin/i) {
> $prefix = '.\';

$prefix = 'perl .\';

> }
> else {
> $prefix = './';
> }
> print "Begin middle\n";
>
> system "$inner.pl";
>
> print "End middle\n";
>
> open my $MIDDLE_LOG, '>', 'middle.log';
> print $MIDDLE_LOG "middle, pid=$$\n";
> close $MIDDLE_LOG;
> ===== end middle.pl =====
>
> ===== inner.pl =====
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> print "In inner\n";
>
> open my $INNER_LOG, '>', 'inner.log';
> print $INNER_LOG "inner, pid=$$\n";
> close $INNER_LOG;
> ===== end inner.pl =====
>
> On Cygwin or Linux, log.txt contains:
> ==========
> outer calling middle
> Begin middle
> In inner
> End middle
> outer after middle
> ==========
>
> On Windows (executing outer.pl from a CMD window), log.txt contains:
> ==========
> outer calling middle
> Begin middle
> In inner

If this were true, you wouldn't have a problem. I suspect a cut and
paste error. 'In inner' should be missing.

> End middle
> outer after middle
> ==========
>

I'm running perl 5.8.8 and got the same behavior you did. I've had
trouble with letting DOS do the file association (running ./outer.pl vs.
perl outer.pl)

--
-brian

Posted by Keith Thompson on June 26, 2008, 8:54 pm
Please log in for more thread options
> Keith Thompson wrote:
> > I want to redirect STDOUT to a file, and have the redirection apply to
> > any programs that I invoke. Here's a brief outline of what I'm doing
> > (full sources to follow):
> > open SAVE_STDOUT, '>&STDOUT' or die "SAVE_STDOUT: $!\n";
> > open STDOUT, '>', 'log.txt';
> > system "some_command";
> > close STDOUT;
> > open STDOUT, '>&SAVE_STDOUT' or die "Restoring STDOUT: $!\n";
[...]
> > if ($^O =~ /^mswin/i) {
> > $prefix = '.\';
>
> $prefix = 'perl .\';
> > }
> > else {
> > $prefix = './';
> > }

[...]

> > On Cygwin or Linux, log.txt contains:
> > ==========
> > outer calling middle
> > Begin middle
> > In inner
> > End middle
> > outer after middle
> > ==========
> > On Windows (executing outer.pl from a CMD window), log.txt contains:
> > ==========
> > outer calling middle
> > Begin middle
> > In inner
>
> If this were true, you wouldn't have a problem. I suspect a cut and
> paste error. 'In inner' should be missing.
>
> > End middle
> > outer after middle
> > ==========

Right on both counts. The "In inner" is missing, and yes, it was a
copy-and-paste error.

> I'm running perl 5.8.8 and got the same behavior you did. I've had
> trouble with letting DOS do the file association (running ./outer.pl
> vs. perl outer.pl)

Running .\outer.pl works for me, but yes, "perl outer.pl" should work
as well.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Similar ThreadsPosted
RE-Redirecting STDOUT September 2, 2004, 11:24 am
Temporarily redirecting STDOUT September 5, 2004, 11:43 pm
Redirecting STDOUT to a file April 25, 2006, 12:53 pm
Redirecting stdout without the use of IO::String May 4, 2007, 3:59 pm
redirecting stdout/stderr to logfile May 24, 2006, 9:59 pm
Redirecting STDOUT to Scalar behaves not as expected. Why? November 27, 2004, 2:24 am
script hangs when run from command line and redirecting stdout and stderr to file January 5, 2006, 12:59 pm
Log::Agent in ActivePerl Windows? March 3, 2005, 6:15 pm
ActivePerl / IIS 6 / Windows Server 2003 SP1 April 15, 2005, 12:13 pm
Tried to install activeperl on windows xp home June 29, 2005, 6:46 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap