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

Okies. I have a dB app that pulls records from mysql.

Intent: if a kill signal (or other) comes along, i want the perl to update the "starttime" of the record back to NULL.

Problem: my current setting of the Signal Handler causes the immediate execution of the Signal Handler.

sub Function { sub UndoStartTime { print "Explicative\n"; my ( $dbh, $ToolTrackingID ) = @_; my $Statement = " UPDATE JobQueue SET StartTime = NULL, PID = NULL WHERE ToolTrackingID = $ToolTrackingID;"; my $sth = $$dbh->prepare ( $Statement ); $sth->execute(); }; local $SIG{TERM} = \&UndoStartTime ($dbh, $ToolTrackingID); . . . # more code } #end Function

So when the main function begins to execute it, as soon as it gets to the "local $SIG{TERM} = " it automatically executes the UndoStartTime function (rather than waiting until it actual gets a Terminate signal). What am I doing wrong?

Replies are listed 'Best First'.
Re: Signals and Functions w/ perameters
by antirice (Priest) on Jun 18, 2003 at 22:26 UTC

    You cannot store parameters with a reference to a sub. One way around this is to do something like this.

    local $SIG{'TERM'} = sub { UndoStartTime($dbh,$ToolTrackingID); };

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      antirice - You deserve a beer =) Thank you that fixed it! WOOOT!
Re: Signals and Functions w/ perameters
by bobn (Chaplain) on Jun 18, 2003 at 22:22 UTC
    It won't call the function right away if you did
    local $SIG{TERM} = \&UndoStartTime;
    instead.

    Also, since when can you nest sub statements?

    --Bob Niederman, http://bob-n.com
      bob - it localizes the function to the parent function only...has been there as long as i have been coding in perl. circa 3 years
        Yer right, I guess - but funny things start happening to the lexical varibles defined inside the outer sub but outside the inner sub - so much so that a warning message exists:
        Variable "$x" will not stay shared at ./nested.pl line 9.
        So I don't do this, and thought it was in fact not allowed.

        --Bob Niederman, http://bob-n.com