Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

We interrupt this program...

by kansaschuck (Sexton)
on Feb 20, 2008 at 19:13 UTC ( [id://669076]=perlquestion: print w/replies, xml ) Need Help??

kansaschuck has asked for the wisdom of the Perl Monks concerning the following question:

Monks, Ok I've got this Perl program running in a loop and life is good as it continually monitors. But I would like to stop it from time to time. Of course if I drop the command prompt window it ends. Looking for something a little nicer. Cntl-C ? I could use some examples or at least finger pointed in the right direction and a gentle push. Interrupt and then offer a resume/continue? all ideas, examples, suggestions appreciated. kc

Replies are listed 'Best First'.
Re: We interrupt this program...
by zentara (Archbishop) on Feb 20, 2008 at 20:06 UTC
    GLib to the rescue. If you put the readkey in a thread, you can control the mainloop thru a shared variable. 's' makes it stop, 'g' makes it go, 'q' makes it exit.
    #!/usr/bin/perl use warnings; use strict; use Glib; use Term::ReadKey; use threads; use threads::shared; $|++; ReadMode('cbreak'); my $go; share($go); # order is important $go = 1; # works non-blocking if read stdin is in a thread my $count = 0; my $thr = threads->new(\&read_in)->detach; my $main_loop = Glib::MainLoop->new; my $timer = Glib::Timeout->add (1000, \&timer_callback, undef, 1 ); # can also have filehandle watches #my $watcher; #$watcher = Glib::IO->add_watch( fileno( $pty ), ['in', 'hup'], \&call +back); $main_loop->run; ReadMode('normal'); # restore normal tty settings sub timer_callback{ #do stuff if($go){ $count++; print "\n$count\n"; } return 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 's'){$go=0} if($char eq 'g'){$go=1} 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
Re: We interrupt this program...
by BrowserUk (Patriarch) on Feb 20, 2008 at 20:42 UTC

    Assuming Win32, get yourself ProcessExplorer, it is what the TaskManager always should have been. One of many nice features is that it allows you to suspend and resume processes.

    Alternatively, if the process produces regular output to the terminal, you can use ^S to suspend and any key to resume. And if you have your Command Prompt windows configured with "Quick Edit" setting, then left-clicking the window will suspend (assuming output) and right-click resume.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      And in the *nix world you can press Control-Z in the command window. Restart the app with fg. (Update: this uses signals SIGSTP/SIGSTOP and SIGCONT -- assuming their default behavior.)
Re: We interrupt this program...
by ikegami (Patriarch) on Feb 20, 2008 at 19:49 UTC
    You could use Term::ReadKey to check if a key was pressed without blocking.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://669076]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-23 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found