SPUG: interrupts!

Jim Flanagan jimfl at tensegrity.net
Thu Aug 29 19:22:51 CDT 2002


On Thu, 29 Aug 2002, Baker, Stephen M wrote:

      > I have an interrupt handler built into a script to
      > guarantee data logging before the program exits in the
      > event an interrupt signal is detected...  However the
      > manner in which I know how to do this does  not permit
      > me to access non-local variables in the interrupt handler
      > function.  The nature of the interrupt handler is that an
      > interrupt triggers a reference to a function call, rather
      > that a system process, and as such the function call is
      > not capable of handling parameters.  Does anyone know a
      > way to add parameters, or make it possible to access them
      > without making them global?

  You can use a closure, like this:

    $SIG{USR2} = sub { handler($foo, $bar, $baz) };

  You can set this closure up anywhere you like (where the parameters you
  want are scoped)

    package One;
    $foo = 1;
    $bar = "Gronk";
    $baz = [1, 2, 3, 4, 5];

    $handler_ref = sub {handler($foo, $bar, $baz)};

    package main;

    $SIG{USR2} = $One::handler_ref;

  Or whatever. It doesn't matter if the parameters change over time. 
  The closure will track them. Like magic.

-- 
Jim Flanagan

    http://jimfl.tensegrity.net
  mailto:jimfl%40t%65ns%65gr%69ty.n%65t


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list