This is alot more complex than an alarm, but you could run the regex in a separate thread, and have a second parallel thread time it out. Note you need a later version of threads, and Thread::Cancel. Just an idea. This allows you to issue keypress commands too :-)
#!/usr/bin/perl # untested and various variable and thread resets ignored use warnings; use strict; use Glib; use Term::ReadKey; use threads; use threads::shared; use Thread::Cancel; $|++; ReadMode('cbreak'); # works non-blocking if read stdin is in a thread my $count:shared = 0; my $done:shared = 0; my $thr = threads->new(\&read_in)->detach; my $thr1 = threads->new(\&long_regex); # only can cancel non-detached threads my $main_loop = Glib::MainLoop->new; my $timer = Glib::Timeout->add (100, \&timer_callback, undef, 1 ); $main_loop->run; ReadMode('normal'); # restore normal tty settings sub timer_callback{ #do stuff $count++; print "\n$count\n"; if ( ($count > 10) or ($done == 1)){ $thr1->cancel(); print "thread canceled\n"; return 0; } return 1; } sub long_regex{ while(1){ sleep 1; } $done = 1; } sub read_in{ while(1){ my $char; if (defined ($char = ReadKey(0)) ) { print "\t\t$char->", ord($char),"\n"; #process key presses here #if($char eq 'q'){exit} if(length $char){exit} # panic button on any key :-) } } } __END__
In reply to Re: How to interrupt regular expression matching?
by zentara
in thread How to interrupt regular expression matching?
by mishuk27
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |