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

Short version of a long story: I have a program that uses a subroutine in a utilities.pm module. The subroutine forks and the child calls a java program that extracts database info and ends; the java bit takes several minutes to run. I have signal handlers set up in the main program and I want to know if they are still active for the child process? Also, it seems the global variables set by the signal handlers are not inherited by the child (and maybe not even by the module before the fork), is there a way to export them from the main program to the module without explicitly passing them? (please don't tell me I'd have to do that for each subroutine in the module---arghh!) Thanks for any insight!
  • Comment on signal handlers & global vars in module subs?

Replies are listed 'Best First'.
Re: signal handlers & global vars in module subs?
by eg (Friar) on Jan 17, 2001 at 09:25 UTC

    Yes, if you fork, both the parent and child should share the signal handler.

    The code in your module is unaware of the "global variables" in main. The best thing to do would be to qualify the variable names in the module with the proper namespace. For example:

    package Foo; sub bar { print $::bar, "\n"; } package main; $bar = "Hello, World!"; Foo::bar();

    Alternatively you can stomp all over your module's namespace with something like (don't do this)

    package Foo; sub bar { print $baz, "\n"; } package main; $baz = "Hello, World"; foreach my $sym ( keys( %{*{::}} ) ) { *{"Foo::".$sym} = *{"::".$sym}; } Foo::bar();

    Note how I had to change $bar to $baz in the second example? That's because if I had left it on $bar, I would have stomped on the old coderef in Foo::bar. See why it's a Bad Idea?

    Leave the variables in main:: and just call them with a ::