I've got some really weird behavior out of Perl in my error handling...

   - My warning/error subroutine (sub PrintError) gets values from the
   caller and copies them to local variables (my).
   - If they have certain values, I change some of these local variables.
   - I display the new values to the browser.  They are the new values.
   - I set global variables to the value of these local variables.  (The
   only place these global variables are set.)
   - I call confess();
   - It calls my handle_errors subroutine.  (All sounds good so far, right?)
   - The global variables now have the ORIGINAL local values, before I
   changed them...?

There are no additional my variables defined later in PrintError.

I don't get it.

I copied the if statement and block from the PrintError into my
set_die_handler (only change is to set the global variables).  And it is
"fixed".  How could it get the original values?

(I'm also getting 2 emails for each error... The only difference is one
$msg has the date-time stamp and the other doesn't.  I'd rather not get
two, but it is ok.)

It "works".... I'm just confused as to why I'm getting older values and
have to repeat the if.

Code follows:

$sth->execute($StudentID) or PrintError ('Database error executing
statement 1.', $sth->errstr, 1, \$HeadOpen, 1);



sub PrintError {
    # All variable names here are unique within the program.
    my ($Message, $Message2, $Die, $HeadOpenRef, $MySendEmail) = @_;
    if (!defined $Message2) {
        $Message2 = '';
    }

    if ($Message eq 'Database error executing statement 1.' && # This is
the block that changes values
        $Message2 =~ m/String data, right truncation/) {
        $Message = 'ID was too long.';
        $MySendEmail = 0;  # Don't email me!
    }

    if (${$HeadOpenRef}) {
        print '<meta http-equiv="REFRESH" content="15; URL=', $retadr,
              '"></head><body>';
        ${$HeadOpenRef} = 0;
        HSNGSANDDB->headclose();
    }
    print ('<h1>', $Message, '</h1><br>');  # It prints the new value
    if ($Message2) {
        print ($Message2, '<br>');
    }
    if ($Die) {
        #print ('SendEmail = ' . $MySendEmail . '<br>');
        print ('</body></html>');
        $ErrorLineNumber = (caller())[2];
        $SendEmail = $MySendEmail;   # These three lines are the ONLY
places these global variables are set.
        $gMessage  = $Message;          #
        $gMessage2 = $Message2;       #
        open STDERR, '>', File::Spec->devnull();
        confess ();
    }
}


    sub handle_errors {
        my $msg = shift;
        if ($SendEmail) {  # Has original value
            my %mail = (
                Smtp => myserver,
                To => myemail,
                From => myemail,
                Subject => 'Error in PrintAContract',
            );
            $mail{Message} = 'Line number: ' . $ErrorLineNumber . "\n";
            $mail{Message} .= 'Message = ' .   $gMessage .  "\n"   if
(defined $gMessage);
            $mail{Message} .= 'Message2 = ' .  $gMessage2 . "\n"   if
(defined $gMessage2);
            $mail{Message} .= "\n" . $msg if (defined $msg);
            sendmail(%mail) or print $Mail::Sendmail::error;
        }
    }
    set_die_handler(\&handle_errors);

$SendEmail, $gMessage, and $gMessage2 have old values when handle_errors is
run.  The original values are in the email that gets sent...

-- 
-- 
You received this message because you are subscribed to the Google Groups 
"NLUG" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nlug-talk?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"NLUG" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to