Home > Archive > PERL Beginners > July 2004 > print to both STDOUT and a file?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
print to both STDOUT and a file?
|
|
| Brian Volk 2004-07-30, 8:55 am |
| Hi All,
Is there a way to print to both the STDOUT and a physical file. other than
../script.pl >>outfile.txt ? Here is what I have now, which works great,
but I would like to see the links (about 2,000) scroll.
---- this works great, but the links don't scroll
open STDOUT, ">>C:/perl/bin/output/links_1.txt"
or die "Can't open links file: $!";
---- this is probably my first step
open my $OUTFILE, ">>C:/perl/bin/output/links_test.txt"
or die "Can't open links file: $!";
---- below is the loop
while (<> ) {
print "$ARGV $1\n" and close(ARGV) and next
if /$RE{URI}{HTTP}{-keep}/;
}
Thanks!
Brian Volk
| |
| Chris Devers 2004-07-30, 8:55 am |
| On Thu, 29 Jul 2004, Brian Volk wrote:
> Is there a way to print to both the STDOUT and a physical file. other
> than ./script.pl >>outfile.txt ? Here is what I have now, which works
> great, but I would like to see the links (about 2,000) scroll.
It's a cop-out, but can't you just have two print statements?
while (...) {
print $OUTFILE "$whatever";
print "$whatever";
}
There's probably a clever & slightly-faster-running way to get one print
statement to fork the output, but it may not be worth it.
Still, if you really want to do this in one statment, it may help to
look at what the `tee` Unix command does: it's a command for doing
exactly what you're asking for, sending the input it receives on
standard input to both the controlling terminal and a specified file.
I've never looked at `tee`'s source code, but I've always assumed that
it too was just using dual output statements.
--
Chris Devers
| |
| Jeff 'Japhy' Pinyan 2004-07-30, 8:55 am |
| On Jul 29, Brian Volk said:
>Is there a way to print to both the STDOUT and a physical file. other than
If you want to make it transparent, use the IO::Tee module. It's not
standard, though.
use IO::Tee;
my $tee = IO::Tee->new(\*STDOUT, ">> log.txt");
print $tee "some line of text\n";
See:
http://search.cpan.org/author/KENSH...Tee-0.64/Tee.pm
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
| |
| Brian Volk 2004-07-31, 3:55 am |
| Jeff "japhy" Pinyan wrote:
>If you want to make it transparent, use the IO::Tee module. It's not
>standard, though.
I using IO::Tee and I think I'm getting close. The output is being sent to
the screen and the phyical output file is being created... but I'm not sure
how to send the @ARGV to the output.txt file as well.
use strict;
use warnings;
use IO::Tee;
use Regexp::Common qw /URI/;
my $dir = "C:/Program Files/OptiPerl/test_dir";
opendir (TEST, $dir) or die "Can't open $dir: $!";
# ----------- load @ARGV for <> operator below ---------------
@ARGV = map { "$dir/$_" } grep { !/^\./ } readdir TEST;
# ----------- $1 returns the entire URL {-keep} --------------
my $tee = IO::Tee->new( ">>C:/perl/bin/output/test.txt", \*STDOUT );
while (<> ) {
print "$ARGV $1\n"
and print $tee "\n"
and close(ARGV) and next
if /$RE{URI}{HTTP}{-keep}/;
}
closedir (TEST);
Thanks!
Brian
| |
| Brian Volk 2004-07-31, 3:55 am |
| Thanks to Greg, I was able to get the result I was looking for, but I am
still curious as to what I was doing wrong w/ the IO::Tee; module.
open SAVESTDOUT, ">>C:/perl/bin/output/test.txt"
or die "Open failed";
while (<> ) {
print SAVESTDOUT "$ARGV $1\n"
and print "$ARGV $1\n"
and close(ARGV) and next
if /$RE{URI}{HTTP}{-keep}/;
}
-----Original Message-----
From: Brian Volk
Sent: Friday, July 30, 2004 12:23 PM
To: 'Beginners (E-mail)'
Subject: RE: print to both STDOUT and a file?
Jeff "japhy" Pinyan wrote:
>If you want to make it transparent, use the IO::Tee module. It's not
>standard, though.
I using IO::Tee and I think I'm getting close. The output is being sent to
the screen and the phyical output file is being created... but I'm not sure
how to send the @ARGV to the output.txt file as well.
use strict;
use warnings;
use IO::Tee;
use Regexp::Common qw /URI/;
my $dir = "C:/Program Files/OptiPerl/test_dir";
opendir (TEST, $dir) or die "Can't open $dir: $!";
# ----------- load @ARGV for <> operator below ---------------
@ARGV = map { "$dir/$_" } grep { !/^\./ } readdir TEST;
# ----------- $1 returns the entire URL {-keep} --------------
my $tee = IO::Tee->new( ">>C:/perl/bin/output/test.txt", \*STDOUT );
while (<> ) {
print "$ARGV $1\n"
and print $tee "\n"
and close(ARGV) and next
if /$RE{URI}{HTTP}{-keep}/;
}
closedir (TEST);
Thanks!
Brian
| |
| Jeff 'Japhy' Pinyan 2004-07-31, 8:55 am |
| On Jul 30, Brian Volk said:
>I using IO::Tee and I think I'm getting close. The output is being sent to
>the screen and the phyical output file is being created... but I'm not sure
>how to send the @ARGV to the output.txt file as well.
You're not printing the right stuff!
>my $tee = IO::Tee->new( ">>C:/perl/bin/output/test.txt", \*STDOUT );
When you print to $tee, it will go to both of those places.
>while (<> ) {
> print "$ARGV $1\n"
> and print $tee "\n"
You're not doing it properly above. You should have written
print $tee "$ARGV $1\n"
> and close(ARGV) and next
> if /$RE{URI}{HTTP}{-keep}/;
> }
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
|
|
|
|
|