An interesting module, and implemented well. As constructive critisim, there's two things I'd like to mention.
First, and simplest, you document a return value of undef on timeout but you actually return 0. This can trip people up so you might want to fix it.
Second, I think that, perhaps, you're handling nested time_limit()'s incorrectly (not that I really know what I mean by "correct"). Consider:
#!/usr/bin/perl -w
use TimeLimit;
$outter1 = time;
time_limit {
$inner1 = time;
time_limit { sleep 7 } 5;
$inner2 = time;
} 3;
$outter2 = time;
$elapsed_outter = $outter2-$outter1;
$elapsed_inner = $inner2-$inner1;
print "Seconds elapsed for outter limit: $elapsed_outter\n";
print "Seconds elapsed for inner limit: $elapsed_inner\n";
The output of this test program is:
Seconds elapsed for outter limit: 5
Seconds elapsed for inner limit: 5
Whereas anyone looking at the outter time_limit() would think that it should return no more than 3s later. (Imagine the inner time_limit() call being burried in a called subroutine...)
Further, what if the code you expected to cause a timeout was after the inner time_limit() call? Because your code actually resets the alarm(), it could take even longer.
Of course, for most applications where you would want to time_limit() some code, you wouldn't really care if it takes a little bit longer to time out than you expected, so it shouldn't matter much. Also, I can't think of a very elegant solution that would handle such cases. I never did understand why they limited it to one alarm per process...
Well, I hope that at least gives you some ideas to work with. In fact, I think I might just have to look into writing a module that would offer a more robust replacement for alarm. :-) And again, congrats on a good module.
Happy coding.
bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are. |