Discussion:
capture output in perl
g***@comcast.net
2005-02-28 13:54:03 UTC
Permalink
hi,
I would like to capture all the output in my perl program during execution. I have code that basically opens and closes an output file multiple times just to write the output. Because I have system calls between lines of code, I have to close the file in order to capture the output from the system calls and write it to the same file (in this case /tmp/tmp.log) .

Not an elegant code. Is there a better way of rewriting the code?

Appreciate any suggestions/comments.

sample code:

open(OUTF, ">>/tmp/tmp.log" ) || die "Cannot write file";
if ($i==10) {
print OUTF "i=10\n\n";
} else {
print OUTF "i!=10\n\n";
}
close OUTF;
system ("ls nofile.txt 2>> /tmp/tmp.log");
...
<some operations>
...
open (OUTF, ">>/tmp/tmp.log" ) || die "Cannot write file";
if ($j==20) {
print OUTF "j=20\n\n";
} else {
print OUTF "j!=20\n\n";
}
close OUTF;

thanks.

Geraldine
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Ankur Gupta
2005-02-28 14:10:49 UTC
Permalink
Post by g***@comcast.net
hi,
I would like to capture all the output in my perl program during execution. I have code that basically opens and closes an output file multiple times just to write the output. Because I have system calls between lines of code, I have to close the file in order to capture the output from the system calls and write it to the same file (in this case /tmp/tmp.log) .
Not an elegant code. Is there a better way of rewriting the code?
Add this to the top:

$| = 1; (To flush the output so that the order of print statement is maintained)
open(STDSAVE, ">&STDOUT"); # Duplicate your STDOUT filehandle incase you want to print something on the screen.
open(STDOUT,">>/tmp/tmp.log"); # Open your log file with STDOUT as the filehandle

#Everything that you print now would go to log file now.
#Along with the output of the system command.

#Now remove all instances of OUTF from the code.
print OUTF "i=10\n\n";
to
print "i=10\n\n";
#Also remove all redirection of the your system commands output to the log file
system ("ls nofile.txt 2>> /tmp/tmp.log");
to
system ("ls nofile.txt 2");

Incase you want to print to the screen.
Use print STDSAVE "HEllo there\n"; # Would print on the screen.
In case you want to close the log file and restore to the normal printing.
close(STDOUT);
open(STDOUT,">&STDSAVE);

One more thing: If you want to print your output to the screen as well as to the log file.
Use open(STDOUT, "| tee /tmp/tmp.log STDOUT");
So whatever you print would go to the screen as well as to the log file. (--> Taken from Perl Cookbook)

Same you can do with the STDERR to capture your warnings and errors to a separate error log file.

HTH
--
Ankur
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Carl Johnson
2005-02-28 20:45:36 UTC
Permalink
Hello group,

I'm trying to read the directory "C:\GIS\wrapped_data" and write record. My scripts is erroring with "can't open directory: no such file or directory. What am I missing?

$outfile = "$infile.txt";
my $dir = "C:\GIS\wrapped_data";
opendir(DIR,"$dir") or die "Can't open $dir directory: $!";
open (OUT, ">$outfile") or die "Error, cannot open file: $outfile. $!";
$record = "";
$index=0;
while ( $numbytes = read(DIR, $record, 1200) ) {
$index++;
if ($numbytes == 1200) {
print OUT "$record";
} else {
die "File Read Error on record $index: $numbytes bytes read; Should be 1200.\n";
}
}
close DIR;
close OUT;




Carl Johnson
Principal Consultant
214-914-9509 - P
214-242-2020 - F
***@crjohnson.net
John W. Krahn
2005-03-01 01:35:44 UTC
Permalink
Post by Carl Johnson
Hello group,
Hello,
Post by Carl Johnson
I'm trying to read the directory "C:\GIS\wrapped_data" and write record.
My scripts is erroring with "can't open directory: no such file or
directory. What am I missing?
$outfile = "$infile.txt";
my $dir = "C:\GIS\wrapped_data";
Perl's double quoted strings interpolate their contents so scalar and array
variables are expanded and escape (backslash-character) sequences are
converted to their special characters. You should use forward slashes in file
paths.
Post by Carl Johnson
opendir(DIR,"$dir") or die "Can't open $dir directory: $!";
Your error message should have printed out the contents of $dir which would
have hinted at the erroneous path name.

perldoc -q quoting
Post by Carl Johnson
open (OUT, ">$outfile") or die "Error, cannot open file: $outfile. $!";
$record = "";
$index=0;
while ( $numbytes = read(DIR, $record, 1200) ) {
You cannot read from a directory handle with read(). Only functions with
'dir' in their names can use a directory handle.

perldoc -f opendir
perldoc -f readdir
perldoc -f rewinddir
perldoc -f seekdir
perldoc -f telldir
perldoc -f closedir
Post by Carl Johnson
$index++;
if ($numbytes == 1200) {
Although you are telling read() that you want 1200 bytes that is only a
suggestion and read() could return anything from 1 to 1200 bytes and still be
valid. By testing for exactly 1200 bytes you could miss some data.

perldoc -f read
[snip]
Attempts to read LENGTH characters of data into variable SCALAR from
^^^^^^^^
the specified FILEHANDLE. Returns the number of characters actually
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
read, 0 at end of file, or undef if there was an error (in the
^^^^
latter case $! is also set). SCALAR will be grown or shrunk so that
the last character actually read is the last character of the scalar
after the read.
Post by Carl Johnson
print OUT "$record";
} else {
die "File Read Error on record $index: $numbytes bytes read; Should be 1200.\n";
}
}
close DIR;
close OUT;
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Wagner, David --- Senior Programmer Analyst --- WGO
2005-02-28 20:52:25 UTC
Permalink
Post by Carl Johnson
Hello group,
I'm trying to read the directory "C:\GIS\wrapped_data" and write
record. My scripts is erroring with "can't open directory: no such
file or directory. What am I missing?
$outfile = "$infile.txt";
my $dir = "C:\GIS\wrapped_data";
With double quotes, what perl is seeing is C:GISwrapped_data which is not what you wnat. Change to single quotes or switch / and it will work as you want.

Wags ;)
Post by Carl Johnson
opendir(DIR,"$dir") or die "Can't open $dir directory: $!";
open (OUT, ">$outfile") or die "Error, cannot open file: $outfile.
$!"; $record = "";
$index=0;
while ( $numbytes = read(DIR, $record, 1200) ) {
$index++;
if ($numbytes == 1200) {
print OUT "$record";
} else {
die "File Read Error on record $index: $numbytes bytes read;
Should be 1200.\n"; }
}
close DIR;
close OUT;
Carl Johnson
Principal Consultant
214-914-9509 - P
214-242-2020 - F
*******************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
*******************************************************
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Mark Taylor
2005-02-28 21:23:19 UTC
Permalink
Hey Carl -

I got in a hurry and did some cut and paste without paying close enough
attention.

The line should read

my $dir = 'C:\GIS\wrapped_data';


I apologize for the error.

Mark
got this error line 9 near "= ="
aborted due to compilation error
Post by Carl Johnson
Hello group,
Hello.
Post by Carl Johnson
I'm trying to read the directory "C:\GIS\wrapped_data" and write
record. My scripts is erroring with "can't open directory: no such
file or directory. What am I missing?
Post by Carl Johnson
$outfile = "$infile.txt";
my $dir = "C:\GIS\wrapped_data";
opendir(DIR,"$dir") or die "Can't open $dir directory: $!";
$outfile. $!";
Post by Carl Johnson
$record = "";
$index=0;
while ( $numbytes = read(DIR, $record, 1200) ) {
$index++;
if ($numbytes == 1200) {
print OUT "$record";
} else {
die "File Read Error on record $index: $numbytes bytes read;
Should be 1200.\n";
Post by Carl Johnson
}
}
close DIR;
close OUT;
Try this, my $dir = = 'C:\GIS\wrapped_data' instead.
HTH,
Mark
*Carl Johnson*
*Principal Consultant*
*214-914-9509 - P*
*214-242-2020 - F*
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Loading...