dsangst has asked for the wisdom of the Perl Monks concerning the following question:

Hello. I'm a newbie here, and have a problem I haven't been able to solve. Where I work we use Korn shell scripts to process files. The scripts are either kicked off by Cron or by a file being posted. These scripts are quite obscure as a result of trying to "fit" procedural programming into a Korn shell (some of it is quite painful!).

I suggested we rewrite the scripts in Perl and I managed to rewrite one script. The problem is, in the Korn shell scripts we did a "set -x" at the beginning of the script and captured all results to a log file. Whenever something fails we check to log to see why the failure occurred. Is there something similar in Perl that does an automatic trace as the program runs?

I've searched this site (and many others) and most people say "use the debugger", which is fine if you actually run the program. But if something fails when it's kicked off by Cron you can't do that (we can't simulate what happened since we don't know what file was posted, etc).

With the Korn script you can look at the log and say "The if statement said the file was empty. Did you guys send and empty file?". I wondered if there was a "set -x" type debugging option that is NOT interactive for Perl without adding print statements to every line of the program.

The closest thing I found here was this thread:, which of course has the advice "use the debugger", although a couple of functions are listed which could do the job but I couldn't get to work.

Any help here would be appreciated. Thanks.

Replies are listed 'Best First'.
Re: Automatic trace statements in program
by Masem (Monsignor) on Mar 20, 2001 at 22:44 UTC
    Your next best friend is warn - if something goes weird, but not enough to kill the script, you can have your script warn "Something's not right, at line " . __LINE__ . " in file " . __FILE__;, and include any other debugging information. This all gets sent to STRERR, which if you are running as a cronjob, will be sent via mail or other options to the user that that job belongs to.
    Dr. Michael K. Neylon - || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Automatic trace statements in program
by I0 (Priest) on Mar 21, 2001 at 00:41 UTC
    PERLDB_OPTS="NonStop=1 AutoTrace=1 frame=2" perl -dS program
Re: Automatic trace statements in program
by AgentM (Curate) on Mar 21, 2001 at 02:21 UTC
    Don't use print statements or warn- what you're looking for is UNIX::Syslog. This is the best, most standardized way to report errors in processes without a controlling terminal.
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Re: Automatic trace statements in program
by orbital (Scribe) on Mar 20, 2001 at 23:40 UTC
    If all your trying to do is write to a specific log file, operations that the program preformed try this:

    open(LOG,">>logfile.log") || die "can't open file: $!\n" select LOG; print "$time batch job completed normally\n"; close(LOG) || die "LOG didn't close: $!\n";

    A few things to note:

    the >> in open refer to opening the file for append, if you want to destroy the file during each execution use a single >

    select followed by a FILEHANDLE sets all commands that normally directed towards STDIN (print, printf write etc..) to point towards the specified FILEHANDLE.

    One other note it is important to error check opening and closing of files, if the file didn't close properly you would want to know before your HD filled up.