nimdokk has asked for the wisdom of the Perl Monks concerning the following question:
I have a dilemma here and have not come up with a decent solution to the problem. Currently our scripts are set to redirect STDOUT and STDERR into a log file. Now I'm sure there are probably problems with the way I have some of this set up, but up until now it has worked rather nicely, for most things. However, we just upgraded from an ancient version of Perl (5.0.4 or something close to that) to Perl 5.8.0 on a Solaris 9 platform.
When I run the following code snippet from the commandline (./programname.pl), it behaves just as I would expect it to up until now. Any output that I insert into the code is included in the log, and any output that the PKZip program generates is captured into the log file. But, when I run this with the -d Debug switch from the commandline (perl -d programname.pl), the output that the program generates is not captured into the file.
For most of the external type programs we use, this is no big deal but we have one program that has a problem if the STDOUT and STDERR are not directed into a file (or to the screen). This gives us false messages saying the program died when in fact it did not. If I comment out the open STDOUT/STDERR lines, things work nicely whether I let all the output go to the screen or into a file (perl -d programname.pl >/path/to/logs/logfile.log 2>&1).
Is there something I need to do when running with the -d flag so that we don't have to modify the script at all in order to run with debug or do I perhaps need to rethink how I'm redirecting STDOUT and STDERR? I've checked through the various docs on the Perl debugger and tried searching here with no real good luck. Here is an example of what I'm working with:
#!/usr/bin/perl use strict; use warnings; use File::Copy; use File::Spec; use Time::localtime; use Fcntl ':flock'; my $log_dir=File::Spec->catfile('/path','to','logs',"my_logfile.log"); my $local_dir=File::Spec->catdir('/path','to','file'); my $file="myzip_file.zip"; my $pkunzip=File::Spec->catfile('/usr','bin','pkunzip'); open (STDOUT, ">$log") or exit 1; open (STDERR, ">>&STDOUT") or exit 2; flock(STDOUT, LOCK_EX | LOCK_NB) or exit 3; truncate STDOUT, 0; #clear out STDOUT to start with a fresh log file. chdir $local_dir or die "Cannot change to $local_dir. $!"; system "$pkunzip -doA $local_dir/$file"; exit;
Now, if the OPEN lines are all commented out so no log file is generated, there is some output sent to the screen. This is fine. If those lines are executed, when the script is called by itself from the commanline, that output shows up in the log file. But not when the script is run with the -d option. Right now, I think our work around is simply to comment out the STDOUT/STDERR redirects when initially testing the script with the debug and then turning it back on when the script is ready to be used in production. Any thoughts?
update: added paragraph breaks, nimdokk
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem redirecting STDOUT/STDERR
by Zaxo (Archbishop) on Jan 16, 2004 at 18:33 UTC | |
by nimdokk (Vicar) on Jan 16, 2004 at 18:41 UTC |