An Introduction to Perl

By Heiko Korsawe, Translation: Thomas Klein

Having a programming language in OS/2 which is easy to learn and quickly set up to perform simple as well as complicated tasks is something surely useful to every user of OS/2. As a home user for instance, you might want to strip certain entries off your HTML documents or change the links to graphic files as you stored them locally. Or maybe you would like to create a find-and-replace procedure that covers multiple files or you just like to enhance your OS/2 by providing your own self made scripts. Using Perl could do the job - even in larger scales when creating complex web pages or running a web server. A big point to mention is Perl's availability on a multitude of platforms (Linux, Unix, OS/2, DOS, Windows) which enables the use of skills acquired, and even programs, across different platforms.

This article is intended as an introduction to programming with Perl for those interested and to encourage you to try your own first steps. The installation manual provided here, along with the download sources should even help beginners in OS/2 to start fiddling with Perl and maybe find a spot on your system to make use of it. Basically, it addresses all you folks ready for take-off without having to go through a lot of theoretical stuff first.

So - why use Perl in OS/2?
Perl is easier to learn than REXX (which is included with OS/2) or C++ and it is basically platform-independent (system subroutine calls are always platform-specific of course). Programs written in Perl under OS/2 are able to work the same way under Linux, Unix, Windows NT and DOS. All it takes is an appropriate Perl Interpreter and the EMX runtime environment - each of them is available for free on the internet. Personally, I'm running EMX version 5 and Perl 5.004_55.

Perl is specifically useful when dealing with text files, thus best suited to run on web servers with CGI scripts, especially when keeping in mind that you're free to change the underlying OS at any time without having to suffer from a loss in functionality of the web server (this is subject to a certain range of OS-specific limitations of course - with Linux there's no trouble so far).

A tiny Perl program
To give you an idea on how easy perl programs are done, we'll use a quick starter shown below. Always remember to provide comments to make it a well documented program - otherwise later it might turn out to be hard to understand what it actually does.

A Perl program source can be written with any text editor (e.g. the OS/2 System Editor). Be aware to start the first row with a # hash sign, as this is used by the Perl interpreter to recognize a Perl script and to determine the standard path of the Perl interpreter. On my system, it looks like this: Next we have the program text, comments are invoked by a leading # on each line.
 * 1) !/tools.os2/emx/bin/perl

Program text: Execution of the program is done by invoking the Perl interpreter and passing the name of the program source file as a parameter.

In an OS/2 windowed session, this will look like...



As you can't try this example on your own without having a functional Perl installed on your machine, I will take this occasion to point you to the download sources and explain the installation process.

Getting it
To use Perl, you'll need the software itself (approx. 7 Megs of ZIP files in whole) as well as the EMX runtime libraries.

These are available at:
 * http://hobbes.nmsu.edu/download/pub/os2/dev/emx/v0.9d/emxrt.zip
 * http://hobbes.nmsu.edu/h-browse.php?dir=/pub/os2/dev/perl

Should the Perl distribution be split into separate ZIP files, you'll have to retrieve: perl_aou.zip perl_blb.zip  perl_exc.zip  perl_inf.zip perl_mam.zip perl_man.zip  perl_mlb.zip  perl_pod.zip perl_sh.zip  perl_ste.zip  perl_utl.zip  plinstl0.zip plinstal.zip plreadme.zip The unzipper program can be found here too:
 * http://hobbes.nmsu.edu/download/pub/os2/util/archiver/unz552x2.exe

This is a self-extracting archive. Simply move it into a directory of your choice and execute it. This will result in the Unzip program being created which you can use to unzip the files that you downloaded in the previous step, that's to say - the Perl distribution files.

Installation
Once you have downloaded the software, you're ready to install. Make sure that your unzip program's path is either part of the system path or that you know the path it resides within so that you're able to execute it fom the command line.

First you'll need to install the EMX runtime. Older libraries of the EMX distribution need to be deleted prior to the install. They are likely to exist - even in part - if being installed by some other application. To get rid of them, you'll have to find them first... use a command like dir c:\emx*.* /s from within an OS/2 window to scan your C: drive or appropriate other drives too. If something was found, look for the files listed below: emx.dll     emxio.dll   emxlibc.dll  emxlibcm.dll  emxlibcs.dll emxwrap.dll emx.exe     emxbind.exe  emxfpemu      emxl.exe emxload.exe emxrev.cmd  emxstack.exe emxupd.exe These files have to be renamed. After ensuring that the old apps still work with the new EMX runtime libraries (the ones you just downloaded), they can be deleted. Next, you'll have to decide where to put the EMX directory. For me, this is C:\TOOLS.OS2\EMX. As the directory EMX is created automatically upon extraction of the ZIP file, you start by placing the ZIP file (emxrt.zip I guess) into the base directory of your choice - like c:\Tools.os2 in my case - and then unzip it. (I'm using UnZip 5.20 of 30 April 1996 for OS/2, but it works with different unzips too, even a DOS executable in worst case). One more hint: If you choose c:\os2\apps as the directory to hold the unzip program, you'll have it available in your system's path without the need to adjust that manually. Okay, now we're ready to unzip the EMX runtime distribution:

Start a windowed OS/2 session cd c:\Tools.os2 unzip emxrt.zip That's it!

Finally you'll have to complete changes to three lines in CONFIG.SYS. Make sure you'll back up its original version first (one never knows...): LIBPATH= ...;C:\TOOLS.OS2\EMX\DLL SET BOOKSHELF= ...;C:\TOOLS.OS2\EMX\PERLLIB\BOOK SET PATH= ...;C:\TOOLS.OS2\EMX\BIN The "..." above stands for the previous contents of each line respectively. The statements shown above are to be appended to the previous content - not replacing it! You'll have to reboot your system to make these changes effective. To check out if the EMX installation is okay, try entering emxrev from within an OS/2 window. Running EMX 0.9d fix 4, this should give you an output like: EMX : revision = 61 EMXIO : revision = 60 EMXLIBC : revision = 63 EMXLIBCM : revision = 64 EMXLIBCS : revision = 64 EMXWRAP : revision = 60 If the values differ, there's still some older DLL lying around somewhere which has to be deleted. Installation of Perl itself is quite simple thanks to its own installation program (plinstal.zip und plinstl0.zip). Those two files have to be unzipped into the same directory that holds the other zip files of the Perl distribution. Then, ChDir into this directory, and run: unzip plinstal.zip unzip plinstl0.zip After this has been accomplished, run install to start the install program. Now you're able to choose the amount of stuff being installed. If you're not sure about it, simply install everything. The most important point here is selecting the correct install directories.









The directories defaults following PERLLIB don't require adjusting.

After installation, the tree structure (using my directory names) looks like this:



If the install program is missing for whatever reason, you'll have to do the install manually. This will be simplified by referring to the information contained in the file README.os2 which will be created upon extraction of plinstl0.zip and plreadme.zip.

...briefly: unzip perl_exc.zip *.exe *.ico -d c:\tools.os2\emx\bin unzip perl_exc.zip *.dll -d c:\tools.os2\emx\dll unzip perl_aou.zip -d c:\tools.os2\emx\bin unzip perl_utl.zip -d c:\tools.os2\emx\bin
 * Perl VIO and PM executables (dynamically linked)
 * Perl_ VIO executable (statically linked)
 * Executables for Perl utilities

Important! You'll have to create c:\tools.os2\emx\perllib prior to these steps. mkdir c:\tools.os2\emx\perllib

unzip perl_mlb.zip -d c:\tools.os2\emx\perllib\lib unzip perl_ste.zip -d c:\tools.os2\emx\perllib\lib\site_perl unzip perl_blb.zip -d c:\tools.os2\emx\perllib\lib unzip perl_man.zip -d c:\tools.os2\emx\perllib\man unzip perl_mam.zip -d c:\tools.os2\emx\perllib\man unzip perl_pod.zip -d c:\tools.os2\emx\perllib\lib Important! You'll have to create c:\tools.os2\emx\book prior to these steps. mkdir c:\tools.os2\emx\book unzip perl_inf.zip -d c:\tools.os2\book unzip perl_sh.zip -d c:\tools.os2\emx\bin You're free to compile this into a script file, like inst.cmd to make it run automatically. Finally, you'll have to adjust some settings in CONFIG.SYS. Append the following SET commands: The setting f:/perllib/lib has to be supplied for compatibility reasons and does not require to actually exist on your system. After this is done, restart your system. Perl should be functional now... check it by running perl -v and testperl The test program should work without popping out error messages. If everything is okay, you should see
 * Main Perl library
 * Additional Perl modules
 * Tools to compile Perl modules
 * Manpages for Perl and utilities
 * Manpages for Perl modules
 * Source for Perl documentation
 * Perl manual in F<.INF> format
 * Pdksh

Programming in Perl
The program text is written with any text editor (e.g. the OS/2-Systemeditor). Select a file name of your choice. You should however consider using a reasonable file name extension like .pl. There are short descriptions of the programs contained as comments.

Program part 1
This represents the first part of each of the follwing 6 sample programs. It contains variable declarations and assignments.

Program part 2
This includes samples of data output. Actually each example is intended to be tried on its own but it also works if combined into one large program (keep in mind that each example requires program part 1 too!). The program is executed by typing perl prog1.pl from within an OS/2 window.













Have fun playing around with Perl!