amboxer21 has asked for the wisdom of the Perl Monks concerning the following question:
is it possible to run a command every x seconds? Lets say i wanted to watch for new lines in a file and if there is a new line, parse the line for a specific word and add that word to a button on a tk UI? Continue to do so as long as new lines are added tot hat file?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: run command every x seconds
by graff (Chancellor) on Feb 27, 2014 at 04:03 UTC | |
In the context of a Tk GUI, you probably want to check out something called "Tk::after" (I'm not sure how to link to an online man page for that, but if you have Perl Tk installed, just run "perldoc Tk::after") | [reply] [d/l] |
by kcott (Archbishop) on Feb 27, 2014 at 07:56 UTC | |
"... "Tk::after" (I'm not sure how to link to an online man page for that, ..." I keep these two bookmarked for quick reference: -- Ken | [reply] |
by amboxer21 (Sexton) on Feb 27, 2014 at 04:48 UTC | |
Thanks this looks very promising. I am having a look now. I'd rather use metacpan than perldoc. https://metacpan.org/pod/Tk::after | [reply] |
by graff (Chancellor) on Feb 27, 2014 at 05:01 UTC | |
Perldoc is salt of the earth, mother's milk, Popeye's Spinach, manna from heaven, food of the gods, chicken soup for the programmer's soul, Breakfast of Champions, … and it's ready to serve your reading pleasure pretty much whenever and wherever perl itself can run. You gotta love it. | [reply] |
|
Re: run command every x seconds
by kcott (Archbishop) on Feb 27, 2014 at 07:25 UTC | |
G'day amboxer21, "is it possible to run a command every x seconds? Lets say i wanted to watch for new lines in a file and if there is a new line, parse the line for a specific word and add that word to a button on a tk UI? Continue to do so as long as new lines are added tot hat file?" Yes. Take a look at these (and related information they link to): There's plenty of examples of using both of those on this site (use Super Search). Important: Using sleep in a Tk application (as suggested in other responses) is a very bad idea because it interferes with the event loop (started by MainLoop). You'll also need the -textvariable option (described in Tk::options) to change the text of your Tk::Button. -- Ken | [reply] [d/l] [select] |
by amboxer21 (Sexton) on Feb 27, 2014 at 19:58 UTC | |
I wasn't going to use sleep in a while loop anyway. Someone in a perl group on Facebook suggested, POE. But I was looking at Tk::After then had to stop because something came up. But I thinks that where I'm gonna start. I had a look at POE and it looked like Tk::After was an easier approach!
| [reply] |
by kcott (Archbishop) on Feb 28, 2014 at 10:33 UTC | |
Here's a working (albeit barebones) example to show the techniques you'll probably need. (See Notes at the end.)
Notes
Update: I had a misplaced closing brace in check_connections() (putting the for loop outside the if (-e $$file_ref) block — it should've been inside that block). This didn't affect how the routine currently functioned but may have introduced a weird bug if subsequent changes were made. Fixed and successfully retested. -- Ken | [reply] [d/l] [select] |
|
Re: run command every x seconds
by LanX (Saint) on Feb 27, 2014 at 03:58 UTC | |
But if I were you I'd have a look at tail -f and it's pure Perl equivalents.
Cheers Rolf ( addicted to the Perl Programming Language)
updateDid you update the TK part of your question? | [reply] [d/l] |
|
Re: run command every x seconds
by basiliscos (Pilgrim) on Feb 27, 2014 at 09:51 UTC | |
If you are using *nix, then you can white your watching-logic as usual perl program, and then launch it via :
By default, watch re-launches the program every 2 seconds, and clears the screen. | [reply] [d/l] |