Gossamer Forum
Home : General : Perl Programming :

A better way to log items?

Quote Reply
A better way to log items?
PROBLEM:
I have a script that checks our database files for human errors (incomplete info, missing data, etc) in the following way:

open flat file, assign lines to variables and run if/then statements to check for mistakes.

example:

if ($field1 eq "") {
print "Error: $field1 is empty. This is a required field\n\a";
}
if ($field2 ne "TX") {
print "Alert: Out of State File - see laws regarding the state of $field2\n\a";
}

This works well and I run about 300-400 various tests. I'd like to log these errors to a file (each file actually has a notes file related).

Here is what I am thinking about doing.
1. Create subroutine called error_log which would open a temporary file (error.log) and append the error to it.
2. Put the &error_log; after the print statement in each if/then (so if an error the file gets opened and appended)
3. At the end of the script open and read the error.log and then delete the contents.
4. Open and sitck the error log contents into the notes file (which I'm already having to update with the fact the file was error checked).

QUESTION:
Is there a better way to do this without have to open and close the error log so many times (actually each file only produces 1 or 2 errors at the most - if any at all) ?

Isn't there some way I can stick all of the errors into a variable and eliminate the need for using error.log as a temporary "holder"?

TIA - Mike.
Quote Reply
Re: [Watts] A better way to log items? In reply to
Quote:
Isn't there some way I can stick all of the errors into a variable and eliminate the need for using error.log as a temporary "holder"?

In case anyone stumbles across this post later here is the answer...

my @error_list;

if ($field1 eq "") {
push (@error_list, "Error: $field1 is empty. This is a required field\n");
}
if ($field2 ne "TX") {
push (@error_list, "Alert: Out of State File - see laws regarding the state of $field2\n");
}

print @error_list;

Now I can stick @error_list where ever I need to.