Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

signal handler in referenced subroutine

by aijin (Monk)
on Aug 03, 2001 at 21:56 UTC ( [id://102068]=perlquestion: print w/replies, xml ) Need Help??

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

I've got a snazzy little script on the go and I've run into a little problem.

First, the background. I'm using a module written by a coworker that can accept a reference to a subroutine as a parameter. The module then runs this subroutine on each string that it returns as output. The referenced subroutine I've written takes each string, separates the values and puts them into an XML file via a homegrown XML module.

My script watches the size of the XML file and how long it's been running, and kills the process after a certain amount of time/size. The problem is, sometimes it gets killed mid-XML file write. This makes the XML invalid, and effectively useless.

What I want to do is install a signal handler in the subroutine that will wait for a second or two to allow the write to complete before killing the process. The problem I'm having is figuring out how to do this. Usually I'd just do the following (ala: The Perl Cookbook):

sub wait_for_it { $SIG{INT} = \&wait_for_it; sleep(3); } sub writing { local $SIG{INT} = \&wait_for_it; #finish writing }
But the problem is that I can only pass in one subroutine. I thought about using an anonymous subroutine, but then realized that I don't know how can I get it to assign itself to the $SIG{INT} within itself.

Admittedly, I'm not all that strong with signals or references, so the solution might be obvious to those who've dabbled in this area before. Guidance is much appreciated.

-aijin

Replies are listed 'Best First'.
Re: signal handler in referenced subroutine
by trantor (Chaplain) on Aug 03, 2001 at 23:38 UTC

    Err... I see a potential design flaw here! Killing a process when a file reaches a certain size or uptime does not make much sense, unless you can justify it with some major requirement, or maybe I didn't understand well...

    TMBAWTDI! (There MUST Be Another Way To Do It!). What about this metacode:

    sub writer_function { if((time - $^T <= $max_uptime) && ($output_size <= $max_size)) { # # Normal processing here # $output_size += $size_written; } else { # # Cleanup, then exit # } }

    Sounds good?

    -- TMTOWTDI

      It's a little tough to explain.

      The module that I call deals with MIBs. If you've ever worked with MIBs you'll know that although there's a standard, it's often ignored, and you can end up in infinite loops (well, until your memory runs out).

      The writer function gets called for each line of output from the module function. If it's looping indefinately, it's going to keep calling the writer function. I'm not sure if I can kill the entire output function from the referenced writer function. If I can, your solution may just work.

      -aijin

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://102068]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-19 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found