Brandon's Notepad

June 5, 2015

Playing WAV Files In PERL

Filed under: Perl,Programming — Brandon @ 3:42 pm
Tags: , , , , ,

Home > My Lists > Programming Notes > PERL > Playing WAV Files In PERL


I don’t remember why exactly, but a few years ago I wrote a countdown timer that would sound a chime when it reached zero. I decided to keep the code that plays the sound for future reference. Note, this works using ActiveState PERL on Windows.



use Win32::Sound;

$wav = 'C:\path\to\file.wav';

Win32::Sound::Play($wav);


June 10, 2013

Interactive PERL Shell Environment

Home > My Lists > Programming Notes > PERL > Interactive PERL Shell Environment


Our first family computer was the IBM PCjr, and as i recall, we had two boot options: DOS 2.1 on 5.25″ floppy or BASIC A on cartridge. That’s right, depending on what we needed to do, we could use BASIC as an operating system, more or less. Bill Joy’s C shell (csh) was written to resemble the C programming language and can be used interactively or as a script interpreter. In more modern times, Python too can be run in either mode. Since I program a lot in PERL, it sure would be nice to have the ability to “live” inside a program, especially for debugging and testing purposes.


The Basis

Actually, it is quite possible to run PERL in an interactive mode. Consider the basic two-liner below that I picked up here from Asim Jalis of MetaProse:

#!/usr/bin/env perl
do{print("perl> ");$_x=<>;chomp $_x;print(eval($_x)."\n")}while($_x ne "q")

In a loop, this program presents a prompt, accepts input, strips off the newline at the end, and then executes (evaluates) the code block, that is until it encounters an input string consisting simply of ‘q’.

I reluctantly included the word ‘shell‘ in the title of this post, since this PERL environment isn’t really a user-interface between the user and the OS kernel, but it does resemble what most of us think of when we hear the word in this context. Moreover, this should not be confused with the Perl Shell, psh.

Of course, I can’t be satisfied with the bare minimum. There are a few bells and whistles I’d like to have. To make room for some additions, I expanded the script as follows:

  1. #!usr/bin/env perl
  2. do {
  3.    print("perl> ");
  4.    $_x=<>;
  5.    chomp $_x;
  6.    print(eval($_x)."\n")
  7. } while($_x ne "q")

Customizations

Recreating History. It’s always helpful to have a listing of the last actions performed. Unix shells usually include two methods for recalling and re-executing commands with or without editing. The first is the use of the up and down arrow keys to cycle through previous commands. This is the same functionality provided by the old DOSKEY command in DOS, which was later built into the command prompt in Windows. While this is a really cool feature, the programming is a bit involved for me to tackle at this time; besides, it would be a wasted effort since running this interactive environment in ActivePERL on Windows (where I actually do the majority of my scripting) magically makes this feature available. I’ve tried this in bash and ksh (set -o vi) on Linux but it did not work.

The second is a ‘history‘ command that simply lists the previous n commands on the screen. The list is usually numbered and a command can be re-executed by typing !n, where n is the line number of the desired command. I doubt I will get that fancy, but I would like to implement a basic history command.

Lazy Line Endings. I thought it might be nice to eliminate the need to end each line in a semicolon, just to make the environment more user-friendly. This too proved to be unnecessary, as the eval() command makes the semicolon optional by default (unless, of course, if multiple lines of code are being stacked end-to-end on the same command line).

I will update the section with more ideas and implementations as they come to pass.


Blog at WordPress.com.