Perl: sleep() and the bell, part II
The tutor follows up on an issue that evolved with the ASCII bell character, ‘\a’, a few posts ago.
Back on January 29 I introduced a Perl program that takes, as input, a waiting time in seconds, then makes three beeps afterwards. The program uses the ASCII bell character, ‘\a’, to make each beep.
The program worked great on Windows; alas, on my Linux (Ubuntu) system, the bell wouldn’t ring. Research revealed that, possibly at several levels in Ubuntu, the ASCII bell has been disabled. One explanation I read was that programmers got tired of hearing it. Whatever the reason, I worried my Ubuntu readers might feel left out in the cold.
However, Linux, like Perl, has “more than one way to do it”:) I recalled from Robert’s Perl tutorial that Perl has commands to start external processes; I’d not yet tried any. At the askubuntu site, a user called Rinzwind pointed out that, on the command line,
aplay /usr/share/sounds/alsa/Front_Center.wav
could be used to play the Front_Center.wav file.
Putting together Robert’s and Rinzwind’s advice, I rewrote the Perl program from Jan 29 to make it open the external process of playing Front_Center.wav, as follows:
#!/usr/bin/perl
$sound=”/usr/share/sounds/alsa/Front_Center.wav”;
#Above: the sound’s full name as a string
print “Hello. Please enter, in seconds, how long to wait\n”;
print “before hearing a test sound.”;
$secs=<STDIN>;
sleep($secs);
print “Okay…here’s the test sound…\n\n”;
exec(“aplay $sound”); #exec opens the external process
This solution worked immediately!:) I hope it works for you as well.
I am impressed by the Ubuntu community’s commentary on this matter. It fortifies my loyalty to Ubuntu as a great operating system.
I’ll be talking more about Ubuntu and Perl in future posts. I may even look into recording an actual bell sound for this program, although the benign Front_Center.wav test sound works fine for now:)
Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.
Leave a Reply
You must be logged in to post a comment.