Perl: sleep() and the bell

To the tutor’s mind, Perl code is so easy to write, with so many neat functions, it’s a great teaching language.  Today we’ll have some fun.

I’ve heard that some people don’t like programming in Perl.  I just can’t see why not.  I admit, I’m not a professional programmer.  However, for my everyday purposes, Perl is the easiest language.  Apparently, there are some things outside its scope;  in such cases I have to use another language.  When I can use Perl, though, I’m instantly more confident.

I suspect Perl’s lack of rules bothers some people.  After all, Perl’s slogan is “There’s more than one way to do it.”  People who love Perl, probably love it for that same reason.

Anyway, when you know exactly what you want to do, and it’s within Perl’s scope, you can often just write a few lines and come up with something surprisingly functional.  Or, surprisingly neat and fun, in this case.

I discovered Perl’s sleep() function yesterday on stackoverflow.com. It’s straightforward: to pause your program, for example, for 15 seconds, you use the command sleep(15).

What about the bell? It’s ‘\a’, meaning “alert”, and is (maybe) the sound you hear when your computer boots. It’s a throwback to the old typewriter days. Back then, the typewriter would “beep” near the end of the line, alerting that you’d soon go off the paper if you didn’t return the carriage. (I’m really showing my age.)

So here’s a little Perl timer which allows you to set the number of seconds before you hear “beep”:


#/usr/bin/perl

print “Hello:) How long before you want to hear three beeps?\n”;
print “Enter the number of seconds, please.”;
$secs=<STDIN>;
sleep($secs);
print “\n\nHere’s the bell: \a\a\a \n”;

I tried this on my Windows and Linux systems. Sadly, I don’t hear the bell on my flavour of Linux (although it might work on yours:)). It does work well on Windows, though; in fact, it can be quite loud. You may want to adjust your volume downward before running the program:)

I’ll be looking deeper into the issue of why the bell doesn’t work (so far) on my flavour of Linux. My findings I’ll share in a coming post:)

Sources:

Robert’s Perl tutorial

McGrath, Mike. Perl in easy steps. Southam: Computer Step, 2004.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Tagged with: , , , , ,

Leave a Reply