This old node reminded me of an anecdote that happened a few years ago. There was this CGI script I inherited that was supposed to send feedback from a web form - in other words, a really simple affair. The customer complained that they kept losing feedback messages sent with the form: their customers followed up on the feedback wondering why nothing happened, and found out their feedback hadn't been received in the first place.

When I looked at the script, the problem was obvious: it did the equivalent of

if(open MAIL, "|/usr/lib/sendmail") { print MAIL <<EOF; content of form EOF } else { print "error message"; }
...end of script.

One close later, the customer was happy... Seems that the script did manage to send the message in some circumstances (probably when the print happened to take place in conjunction with a buffer flush), which mislead the the original author.

The lesson: "good enough" isn't. Even when it appears to work.

Replies are listed 'Best First'.
Re: Close your filehandle - a simple fix for a cgi script
by hipowls (Curate) on Jan 28, 2008 at 02:03 UTC

    Isn't it a nice feeling when you fix something like that;)

    Using a lexical variable for the file handle also works.

    if(open my $MAIL, "|/usr/lib/sendmail") { print $MAIL <<EOF; content of form EOF } else { print "error message"; }
    The handle goes out of scope and is closed automatically.

Re: Close your filehandle - a simple fix for a cgi script
by Prof Vince (Friar) on Feb 01, 2008 at 08:52 UTC
    The lesson: as hipowls demonstrated it, always use lexical filehandles.