Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use warnings;
- use Continuation::Escape;
- sub timeout {
- my ($time, $code, @args) = @_;
- my $wantarray = wantarray;
- print "woot\n";
- my @res;
- my $timed_out = call_cc {
- my $escape = shift;
- local $SIG{'ALRM'} = sub { print "catched\n"; $escape->(1) };
- alarm $time;
- if ( $wantarray ) { @res = $code->( @args ) }
- elsif ( defined $wantarray ) { $res[0] = $code->( @args ) }
- else { $code->( @args ) }
- alarm 0;
- return 0;
- };
- die "Exception::Timeout" if $timed_out;
- return $wantarray? @res : $res[0];
- }
- my $timeout = 3;
- my @res = eval { timeout( $timeout, \&foo ) };
- print "$@" if $@;
- print "success\n";
- sub foo {
- for ( my $i = 0; $i < 100; $i++) {
- eval "sleep 1; 1" or print "$@"; print "$i\n";
- }
- }
Add Comment
Please, Sign In to add comment