Fellow monks,

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


"Ex Libris un Peut de Tout"

In reply to Problem redirecting STDOUT/STDERR by nimdokk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.