Expensive Egg-Timers
If you use a CLI you might have done something along the line.
sleep 1m 30s; do-the-next-thing
I have a script called OK
that will display a short text in a hopeful green and morse code O-K via the PC speaker. By doing so I turn my computer into an expensive egg-timer.
As of late I found myself waiting for longer periods of time and was missing a count-down so I could estimate how much more time I can waste playing computer games. The result is a program called count-down.
Since I wanted to mimic the behaviour of sleep
as closely as possible I had a peek into its source-code. That made me realise how lucky I am to be allowed to use Perl 6. If I strip all the extra bits a count-down needs I’m at 33 lines of code compared to 154 lines of GNU sleep
. The boilerplate I have is mostly for readability. Like defining a subset called Seconds
and a Rexex called number
.
Errors in the arguments to the script will be cought by the where clause in MAIN
s signature. Since there are no further multi candidates for MAIN
that might interfere, the usage message will be displayed automatically if arguments are not recognized. Pretty much all lines in the C implementation deal with argument handling and the fact that they can’t trust their arguments until the last bit of handling is done. With a proper signature a Perl 6 Routine can fully trust its arguments and no further error handling is needed. Compared to the C version (that does a lot less) the code can be read linear from top to bottom and is much more expressive. After changing a few identifiers I didn’t feel the need for comments anymore. Even some unclear code like the splitting on numbers and keeping the values, becomes clear in the next lines where I sum up a list of seconds.
Now I can comfortably count down the rest of a year that was made much better by a made better Perl 6. I wish you all a happy 2018.
-
January 1, 2018 at 23:022018.01 Perl 6 Alerts | Weekly changes in and around Perl 6