Another simple method is to use select, ie:
select(undef undef undef, 2);
You can also use signals:
eval
{
local $SIG{'ALRM'} = sub{die 'timesup';};
alarm 2;
# Now do something as a pause
while(1)
{
select(undef,undef,undef,0.01);
}
alarm 0;
};
if($@ =~ 'timesup')
{
# Call timeout code
}
On the face of it, you gain nothing in the second example. However, you can use this to timeout pieces of code should you not want to hang around forever. Gaining a lock on a file for example.
There are some platform issues using SIG{ALRM}, alarm() and select() but you should be ok on most platforms.
HTH
Simon
PS: I was eating toast when I posted this so please excuse any typos :P