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

Hey, I'm kinda newish to perl and I'm trying to figure out how to make a program run in realtime. By that I mean for example, diplaying a clock time that is constantly updating even though the rest of the program might require input from the user to continue (like a clock during a chess game or what-not). Someone told me you could use buffer's to pull this of, but well, I'm an idiot :P. Any help or code snippets would be appreciated. Thanx.

Replies are listed 'Best First'.
Re: Running in 'RealTime'
by PodMaster (Abbot) on Oct 08, 2002 at 07:37 UTC
    Well, regardless of what kind of program it was, GUI "toolkit" or commandline, you'd probably wanna use fork (some form of which any decent GUI will already do).

    you wanna read perldoc perlfork, and do something like (this being the simple example, probably not useful)

    use strict; $| = 1; # disable buffering, read perldoc perlvar my $pid = fork; die "couldn't fork" unless defined $pid; if($pid == 0) { # it's a kid print "type:"; while(<STDIN>){ print qq{you typed "$_"\n\ntype:"}; } } else { while(1) { print "\n\t\t",scalar localtime,"\n"; sleep 1; } }
    GUI toolkits such as wxPerl and perlTk work much better for doing things like this, since well, they are gui tools. There is also various Curses modules on CPAN, for creating console gui programs (before windows, like dos), but I can't run any on windows ;)

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Running in 'RealTime'
by BrowserUk (Patriarch) on Oct 08, 2002 at 12:16 UTC

    Sihal is absolutly right. I read the bit about Chess and thought the op wanted a elapse time clock like used in chess clocks.

    An No alternative to forking is to use Term::ReadKey.

    #! perl -sw use strict; use Term::ReadKey; use Time::HiRes qw(gettimeofday tv_interval usleep); my $start = [gettimeofday]; usleep 10_000, printf "\r%6.2f Press Enter when ready", tv_interval $start while !defined ReadKey(-1); print $/, 'You took ', tv_interval($start), ' seconds to hit the Enter + key', $/; __DATA__ # Ouput C:\test>203582 7.33 Press Enter when ready You took 7.331 seconds to hit the Enter key C:\test>

    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
      Hum, I don't think this answers correctly to the problem... However the first solution might work, but isn't there any other way, apart from forking?
Re: Running in 'RealTime'
by Thelonius (Priest) on Oct 08, 2002 at 17:29 UTC
    If you are using an event-driven system like Tk, you can set timers to run a subroutine at regular intervals. There are several examples in Mastering Perl/Tk
Re: Running in 'RealTime'
by fglock (Vicar) on Oct 08, 2002 at 18:12 UTC

    This is a small example of a "real-time" subroutine:

    # calls this sub each 3 seconds... local $SIG{ALRM} = sub { print "alarm "; alarm 3; }; $| = 1; # no output buffer alarm 3; # start subroutine timer # main program for (1..100) { print "running "; sleep 1; }