Hi all,
This is my virgin question to perlmonks!
I am aiming to write a funtion that impelements a timeout facility.
I have done all the code for the timeout with "eval" "$SIG{ALRM}" "alarm" and I have proven the actual timeout code.
The alarm gets set to a number of seconds, then the next bit of code is where I perform an operation that might hang the script.
What I would like to do is re-use the timeout code by putting it into a timeout funtion, where I pass in the timeout time, address of a funtion and any parameters that need to be passed to that funtion.
In the timeout function, I intend to do the "eval" "$SIG{ALRM}" "alarm" code and then call the funtion that the timeout funtion has been given the address of via the \&<function name> mechanism. This \&<function
name> would be a function that might hang.
E.G.
sub timeout_func{
# The first parameter is the timeout time
my $TIMEOUT=sprintf("%s",$_[0]);
# The second parameter is the address of the function we
# need to perform a timeout for
my $ADDRESS=sprintf("%s",$_[1]);
# The third parameter is the first parameter of the
# function we need to run
my $FIRST=sprintf("%s",$_[2]);
eval{
#set the callback for the alarm signal
local $SIG{ALRM} = sub { die ("ETIME\n") };
#specify the alarm timeout
alarm($TIMEOUT);
#call the funtion that the calling code has provided
#the address of via the \&<function name> mechanism
#if we came back with in the time limit, reset the alarm
alarm(0);
};
} # timeout_func
timeout_func(120,\&funtion_that_might_hang,<1st param to
funtion_that_might_hang>);
I realise that I stil need to do more work on this code, but the bit I
am hoping that someone can advise me on how to do the following:
#call the funtion that the calling code has provided the
#address of via the \&<function name> mechanism
Please could anybody let me know if they know how I should do this.
I know that the \&<function name> mechanism can be made to work becasue the threads and forks modules use this method.
If I can do this, then I can use this timeout funtion in various places in my scripts without duplicatind the same code.
Thanks in advance.
Richard Thomas