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

I want to write a program that works like a stopwatch. It starts counting until you press return. Then the timing stops until it is pressed again. I would prefer if the time showed up counting in the terminal as well.

Replies are listed 'Best First'.
Re: Stopwatch program
by Crulx (Monk) on Jan 19, 2000 at 05:27 UTC
    Time will be your friend here. Something like
    use strict; my ($start_time, $end_time, $time_used); my $input; print "Hit enter to start\n"; while (<STDIN> !~ /q/) { $start_time = time; print "Hit enter to stop\n"; $input = <STDIN>; $end_time = time; $time_used = $end_time - $start_time; print "That took $time_used\n"; print "Hit enter to start, q <enter> to quit\n"; }

    This uses time so it only has 1 sec granularity. This means that for small time values The error involved in counting could be a large part of the result. To display to the screen, you would have to use non blocking io to get the start and stop enters. Hope this gives you some direction. Crulx