redirecting stderr and stdout


J

Jon Landenburer

It seems that no matter how thorough our error checking gets we always
hit a new error not expected. SO I decided trying to ty STDERR, STOUT
and a logfile all together.

This seems to work. Even sytem errors or DBI erros get caught and
resirect. Out problem is htat the order of the statements written to
the output file does not match the order in the code. SO we we tried
flushing all file handlers. to no avail

Code
#!/bin/perl
use DBI;

open (STDOUT, ">dog") or "die cannot open STDOUT to dog\n";
print "begin\n";
close STDOUT;
open (STDOUT, ">>dog") or die "cannot open STDOUT to dog\n";
$| = 1;
open (STDERR, ">>dog") or die "cannot open STDERR to dog\n";
$| = 1;
open (LOG, ">>dog") or die "cannot opent sonnabinny\n";
$| = 1;

print LOG "logme ", $cnt++, "\n";
print STDERR "logerr ", $cnt++, "\n";
print STDOUT "logout ", $cnt++, "\n";
print LOG "logme ", $cnt++, "\n";
print STDERR "logerr ", $cnt++, "\n";
print STDOUT "logout ", $cnt++, "\n";
print LOG "logme ", $cnt++, "\n";
print STDERR "logerr ", $cnt++, "\n";
print STDOUT "logout ", $cnt++, "\n";
print LOG "logme ", $cnt++, "\n";
print STDERR "logerr ", $cnt++, "\n";
print STDOUT "logout ", $cnt++, "\n";
print LOG "logme ", $cnt++, "\n";
print STDERR "logerr ", $cnt++, "\n";
print STDOUT "logout ", $cnt++, "\n";
print LOG "logme ", $cnt++, "\n";
print STDERR "logerr ", $cnt++, "\n";
print STDOUT "logout ", $cnt++, "\n";

@a = `cp sdjkfhjkh sdfsdf`;
system ("cp sdsdsdsdsd sdsdsdsdd");

$database = $ENV{ORACLE_SID};
$dbh = DBI->connect("dbi:Oracle:$database", "gueST_ID",
"GUEST_PWD");


Output
:!cat dog
begin
logerr 1
logout 2
logerr 4
logout 5
logerr 7
logout 8
logerr 10
logout 11
logerr 13
logout 14
logerr 16
logout 17
cp: cannot access sdjkfhjkh
cp: cannot access sdsdsdsdsd
DBI->connect(COMSYST) failed: ORA-01017: invalid username/password;
logon denied (DBD ERROR: OCISessionBegin) at a.pl line 37
logme 0
logme 3
logme 6
logme 9
logme 12
logme 15

I would have expected all of the lines to be written in the order of
the number $cnt
see how everthing to the LOG file handler comes at the end?
Is there a way of avoiding this?
 
Ad

Advertisements

J

Joe Smith

Jon said:
open (STDOUT, ">dog") or "die cannot open STDOUT to dog\n";
print "begin\n";
close STDOUT;
open (STDOUT, ">>dog") or die "cannot open STDOUT to dog\n";
$| = 1;
open (STDERR, ">>dog") or die "cannot open STDERR to dog\n";
$| = 1;
open (LOG, ">>dog") or die "cannot opent sonnabinny\n";
$| = 1;

Get rid of the three lines that set $| and replace them with
select LOG; $| = 1;
select STDERR; $| = 1;
select STDOUT; $| = 1;

Just be sure to have STDOUT selected at the end.

Another way to do it is:

open STDOUT, ">>", "dog" or die "cannot append to 'dog': $!\n";
open STDERR, ">&STDOUT" or die "cannot dup STDERR to STDOUT: $!\n";
select STDERR; $| = 1;
open LOG, ">&STDOUT" or die "cannot dup LOG to STDOUT: $!\n";
select LOG; $| = 1;
select STDOUT; $| = 1;

-Joe

P.S. Next time, look at "perldoc -f open" first, and post to
comp.lang.perl.misc instead of comp.lang.perl .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top