An Introduction to Perl
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.
#!/tools.os2/emx/bin/perl #A simple example to show #how programs are written in Perl #1. Assigning a value to a variable #simple variables names always start with $ #This assigns the value of 1 to a variable called $a $a = 1; #Almost every command in Perl is terminated by a ; #except for comments and loop commands #This assigns the value of 3 to a variable called $b $b = 3; #Now we'll output the values, add them into a third variable and output the result too print "variable 1: $a\n" print "variable 2: $b\n" $c = $a + $b; print "total: $c\n" # \n represents a line break #And that's it.
Execution of the program is done by invoking the Perl interpreter and passing the name of the program source file as a parameter.
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.
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
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:
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.
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
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
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
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.
- Perl VIO and PM executables (dynamically linked)
unzip perl_exc.zip *.exe *.ico -d c:\tools.os2\emx\bin unzip perl_exc.zip *.dll -d c:\tools.os2\emx\dll
- Perl_ VIO executable (statically linked)
unzip perl_aou.zip -d c:\tools.os2\emx\bin
- Executables for Perl utilities
unzip perl_utl.zip -d c:\tools.os2\emx\bin
Important! You'll have to create c:\tools.os2\emx\perllib prior to these steps.
- Main Perl library
unzip perl_mlb.zip -d c:\tools.os2\emx\perllib\lib
- Additional Perl modules
unzip perl_ste.zip -d c:\tools.os2\emx\perllib\lib\site_perl
- Tools to compile Perl modules
unzip perl_blb.zip -d c:\tools.os2\emx\perllib\lib
- Manpages for Perl and utilities
unzip perl_man.zip -d c:\tools.os2\emx\perllib\man
- Manpages for Perl modules
unzip perl_mam.zip -d c:\tools.os2\emx\perllib\man
- Source for Perl documentation
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.
- Perl manual in F<.INF> format
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::
set PERL_BADLANG=0 set PERLLIB_PREFIX=C:\TOOLS.OS2\EMX\PERLLIB\LIB set PERL_SH_DIR=C:\TOOLS.OS2\EMX\BIN set MANPATH=C:\TOOLS.OS2\EMX\PERLLIB\MAN set PERL_BADLANG=0 set PERLLIB_PREFIX=f:/perllib/lib;C:/Tools.OS2/emx/PERLLIB/lib set PERL_SH_DIR=C:\Tools.OS2\emx\BIN
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
The test program should work without popping out error messages. If everything is okay, you should see
[C:\]testperl ECHO is OFF. Testing perl... Getting version... ECHO is OFF. This is perl, version 5.004_55 built for os2 Copyright 1987-1997, Larry Wall OS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel Version 5 port Copyright (c) 1994-1997, Andreas Kaiser, Ilya Zakharevich Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5.0 source kit. ==================================== We checked that Perl can be loaded. Apparently, your PATH and LIBPATH are set to reasonable values. From now on the tests are coded in Perl. Press <ENTER>: ==================================== The next step is to check that you installed shell in a location perl can find, so that you can start external programs. I could find your shell, its version is '@(#)PD KSH v5.2.7 96/06/04' Press <ENTER>: ==================================== Ouph, the most frequent problem is behind... Now testing Perl library search. Found Perl library. Fine... Press <ENTER>: ==================================== The following entries in 'C:/Tools.OS2/emx/PERLLIB/lib/os2/5.00455/Config.pm' contain path settings which look suspicious: archlibexp => 'C:/Tools.OS2/emx/PERLLIB/LIB/os2/5.00455' installarchlib => 'C:/Tools.OS2/emx/PERLLIB/LIB/os2/5.00455' installprivlib => 'C:/Tools.OS2/emx/PERLLIB/LIB' installprivlib => 'C:/Tools.OS2/emx/PERLLIB/LIB' libpth => 'f:/emx.add/lib f:/emx/lib D:/DEVTOOLS/OPENGL/LIB f:/emx/lib/mt' privlibexp => 'C:/Tools.OS2/emx/PERLLIB/LIB' archlib => 'C:/Tools.OS2/emx/PERLLIB/LIB/os2/5.00455' bin => 'C:/Tools.OS2/emx/BIN' binexp => 'C:/Tools.OS2/emx/BIN' full_sed => 'F:/EMX.ADD/BIN/sed' installbin => 'C:/Tools.OS2/emx/BIN' installman1dir => 'C:/Tools.OS2/emx/PERLLIB/MAN/man1' installman3dir => 'C:/Tools.OS2/emx/PERLLIB/MAN/man3' installscript => 'C:/Tools.OS2/emx/BIN' installsitearch => 'C:/Tools.OS2/emx/PERLLIB/LIB/SITE_PERL/os2' installsitelib => 'C:/Tools.OS2/emx/PERLLIB/LIB/SITE_PERL' libc => 'f:/emx/lib/mt/c_import.lib' make => 'F:/EMX.ADD/BIN/make' man1dir => 'C:/Tools.OS2/emx/PERLLIB/MAN/man1' man1direxp => 'C:/Tools.OS2/emx/PERLLIB/MAN/man1' man3dir => 'C:/Tools.OS2/emx/PERLLIB/MAN/man3' man3direxp => 'C:/Tools.OS2/emx/PERLLIB/MAN/man3' perlpath => 'C:/Tools.OS2/emx/BIN/perl' privlib => 'C:/Tools.OS2/emx/PERLLIB/LIB' rsx => 'f:/zax/bin//rsx.exe' scriptdir => 'C:/Tools.OS2/emx/BIN' scriptdirexp => 'C:/Tools.OS2/emx/BIN' sitearch => 'C:/Tools.OS2/emx/PERLLIB/LIB/SITE_PERL/os2' sitearchexp => 'C:/Tools.OS2/emx/PERLLIB/LIB/SITE_PERL/os2' sitelib => 'C:/Tools.OS2/emx/PERLLIB/LIB/SITE_PERL' sitelibexp => 'C:/Tools.OS2/emx/PERLLIB/LIB/SITE_PERL' strings => 'f:/emx/include/string.h' sysman => 'F:/MAN/man1' timeincl => 'f:/emx/include/sys/time.h ' usrinc => 'f:/emx/include' Press <ENTER>: ==================================== You DID NOT edit 'C:/Tools.OS2/emx/PERLLIB/lib/site_perl/os2/Net/Config.pm', You may have a lot of problems with networking... Please edit this file. Tests finished. Press <ENTER>: [C:\]
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.
#!/tools.os2/emx/bin/perl #An example to show how programs #are done in Perl #1. variable assignments #simple variable names always start with a $ #assigning a value of 1 to the variable named $a $a=1; #almost each Perl command is terminated by a ; #except for comments and loop commands #assigning a value of 3 to the variable named $b $b=3; #A literal (static text) is enclosed by either " " or ' ' $c="Don Vito" $d='the "godfather" '; $A="Note that;" $B="variable names" $C="in Perl are" $D="case sensitive"
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!).
#2. Data output #Here we are - now it's time to display some values #The print (to screen) command is quite simple. #\n gives you a line break, but it only works when being #enclosed in " " *not* when using ' ' print "\n---------------------------------------------------\n" print "Example 1\n" print $a; print $b; print $c; print $d;
The program is executed by typing
from within an OS/2 window.
#It can be used without quotes too. In this case, make sure #that you used the . (dot) as concatenation character if #multiple strings are to be displayed - otherwise it won't work. print "\n---------------------------------------------------\n" print "Example 2\n" print $a.$b.$c.$d;
#Of course, multiple values can be displayed using " " #or ' ' without having to rely on the concatenation sign. This #will also allow you to put spaces into the displayed text. print "\n---------------------------------------------------\n" print "Example 3\n" print "$a $b $c $d"
#I guess it looks better if using line breaks (\n). print "\n---------------------------------------------------\n" print "Example 4\n" print "$a\n$b\n$c $d\n" print "$A $B $C $D\n" print "$a\n$b"
#The print command even allows simple calculation functions print "\n---------------------------------------------------\n" print "Example 5\n" print $a+$b;
#Perl automatically detects whether a variable contains #string data or numeric values print "\n---------------------------------------------------\n" print "Example 6\n" print "$b\n" #now increase the value of $b by 10 $b=$b+10; print "$b\n" #We're about to 'attach' a text to the value of $b. This turns $b #into a string type variable $b=$b."is no longer numeric" print "$b\n" #but... if we increase $b's value by 10 again, Perl will #drop the text part of $b and perform the calculation! $b=$b+10; print "$b\n"
Have fun playing around with Perl!