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

Good day perl monks.
I know how assign a simple function for an alarm ($SIG{ALRM}=\&funcName).
And how to make this with a class method?

Replies are listed 'Best First'.
Re: $SIG{ALRM} with a class method
by pg (Canon) on Dec 20, 2002 at 02:12 UTC

    The answer is to use anonymous sub. This happens quite often with Tk, if you use it.

    $mw->Button(command => \&some_func)->pack();
    What if the func takes parameter(s), or the func is a class method. The answer is still the same, anonymous sub, you can just do:
    $mw->Button(command => sub {$obj->some_func($param1, $param2)})->pack( +);
Re: $SIG{ALRM} with a class method
by djantzen (Priest) on Dec 20, 2002 at 03:03 UTC

    You can call a class method like a function in a manner very similar to what you have with this:

    $SIG{ALRM} = \&Classname::method; # update: no parens, force of habit. + Thanks bbfu.

    However, the class name will not be prepended to the argument stack, which class methods very often expect will be the case. So as others have suggested, the approach you probably want is to use an anonymous sub:

    $SIG{ALRM} = sub { Classname->method() };

    Now you can use variables in the new handler, like in the first note in this thread, just so long as they are in visible scope of the handler. So, a global var, or a lexical in the enclosing scope will be accessible.

Re: $SIG{ALRM} with a class method
by batkins (Chaplain) on Dec 20, 2002 at 01:35 UTC
    you could try
    $SIG{ALRM} = sub { $obj->funcName; };
    definitely untested, maybe unintelligent...
Re: $SIG{ALRM} with a class method
by Nygeve (Acolyte) on Dec 20, 2002 at 03:13 UTC
    Thanks for your help, this is what i needed.