Update 10 minutes later.... fixed a couple of code bugs

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__

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

In reply to Re: How to interrupt regular expression matching? by zentara
in thread How to interrupt regular expression matching? by mishuk27

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.