[OS/2 Fixpacks On CD]
Rexx Newbies- by Chris Wenham
OS/2 e-Zine!

Chris Wenham is the Editor In Chief of OS/2 e-Zine! -- a promotion from Senior Editor which means he now takes all the blame.

Blast Back! Send a private message directly to Chris Wenham with your thoughts:

Contact OS/2 e-Zine!

Go to a Printer Friendly version of this page

Summary: As the first in a limited-run series intended to help newcomers get up to speed with Rexx, we introduce some of the benefits of Rexx, examples of how you can put it to productive use in your daily work, and your first few lines of Rexx code.

Second only to the Workplace Shell in terms of flexibility and usefulness is OS/2's built-in scripting language, Rexx. As an interpreted language that doesn't have strict type checking (the enforced difference between, say, number and character variables) and follows an easygoing syntax, Rexx is very easy to learn. If you've ever learned enough about plumbing to fix a leaky faucet or install a new bathroom sink, then you've probably got what it takes to learn a little bit of Rexx and make life under OS/2 much easier.

This is a limited-run series of articles that will go on through the next few issues of OS/2 e-Zine!, teaching the fundamentals of Rexx and giving some examples of how it can be used to automate simple everyday tasks and enhance Rexx-enabled applications that you may already own. It's written to both stand on its own and serve as a "stepladder" to the more advanced techniques found in Dr. Dirk Terrell's monthly Rexx Files.

What Is Rexx Useful For?

To give you an idea of what Rexx is useful for, consider these example scenarios:

  1. Your favorite web page editor cannot "publish" pages to the web server. So you use a few lines of Rexx to give it that capability or at least the closest approximate.
  2. You want an anti-virus check performed on a regular schedule, but you also want the results e-mailed to you.
  3. You want your Internet chat program to keep a log of anything said to you or about you in the chat rooms that you frequent, in case you miss anything while you're away from the screen, or want to double check something weeks or months later.
  4. You need to update records in your database program with cells from your spreadsheet, or vice versa.
  5. You're looking to buy a new piece of equipment and the current price for it is advertised on a web page. You want something that will check that web page regularly and notify you when only the price changes, but not when other unimportant parts of the page change.
  6. Your favorite e-zine is distributed in offline format, and you want to automatically download and unpack each issue as the e-mail reminder arrives.
All of the above examples are not only possible, but almost trivially easy to do with only a few lines of Rexx. In the coming months I'll be describing how to do all of them and more. Rexx is often used in one of two ways: To write stand-alone programs that you launch from the command line or a desktop icon, or as macros that enhance other applications.

An example of a stand-alone Rexx program (which I'll call a "Script" from now on) that you've probably come across dozens of times before is the one that creates an icon on the desktop for the shareware programs you download.

An example of a Rexx program used to enhance another application (which I'll call a "Macro") are the examples that ship with Photo>Graphics Pro and Mesa 2. These macros are not designed to run on their own, but will often automate multi-step procedures in the programs you use (such as setting up a default letterhead or template). Most of the example scenarios described above would be incarnated as the second type; the macro. These are often the most useful and the ones that most beginners seek to learn Rexx for, as it helps them expand the capabilities of programs they're already familiar with.

What Are The Strengths Of Rexx As A Macro Language?

Consider the situation on the Windows platform. Since there is no default macro language there, application developers are faced with the nontrivial and expensive job of designing and creating their own language from scratch whenever they want to add macro features to their programs. These languages, for both copyright, economic and subjective reasons, are nearly always different from program to program. The SALT language you learned for your terminal program is different from the Visual Basic for Applications that you learned for the office suite, which is different from the Word Perfect macro language you grew up with, which is different from Corel's language and LotusScript and so-on and so-on. You might also have noticed that for all these dozens of languages that all do pretty much the same thing anyway, you're giving up 1-2 megabytes of disk space each.

Not so under OS/2. For an OS/2 application developer, adding basic Rexx support to a program is an afternoon's work. What a program is doing when it runs your Rexx macro is simply handing it over to OS/2's built-in interpreter and forgetting about it. The program might also expose a few of it's own internal functions to OS/2 for you to make use of within the macro. Functions to make the host application load or save a file, functions to make a database flip to the next record, update a field, or print a report. This is how a Rexx macro can control the program that launched it. Plus, since adding Rexx support is so easy, even tiny programs come with it. Take HTML-Ed for example: the program is just over a measly 100K, yet it comes with full fledged Rexx macro support. Possible because the author of HTML-Ed didn't have to write his own Rexx interpreter.

For you it means no redundant code and only one language to learn. Once you've mastered Rexx in the course of customizing your spreadsheet, you can apply the same skills to your database, chat client, or any other program that supports Rexx. Better yet, as OS/2's Rexx interpreter gets upgraded and improved (such as when Object Rexx was introduced, or whenever you download one of the many third-party function libraries), then all of your applications are instantly enhanced too. Download a library of functions for sending files over the Internet, and instantly you can add these features to every Rexx empowered program you own.

What Are The Strengths Of Rexx As A Language On Its Own?

As mentioned at the beginning, Rexx is very easy to learn. The syntax is relaxed, not cryptic or heavily symbolic, and is very forgiving of "loose" code that's been written in a hurry.

Rexx is very good at "string processing", or the manipulation of phrases and words in a sentence or paragraph of text. This makes it especially useful in filtering (looking for catch phrases or keywords), filling templates and generating automatic responses to things like e-mail.

And Rexx is good at manipulating files, making it ideal for housecleaning jobs like deleting old log files and automating backups.

Wherever Rexx lacks in some areas, there's often a plug-in library of functions to make up for it. For example, Rexx on its own has only basic and rudimentary math functions, but as shown in Dirk Terrell's Rexx Column, you can download a library of extended math functions easily. There's more where that came from, as will be shown later.

Your First Lines Of Rexx

Assuming you're a complete beginner, we'll start with the complete basics. Rexx is not a fancy-shmancy language like C where you have to import a slew of libraries just to make it say "Hello." To do a simple "Hello, World" program in Rexx, launch a text editor such as the OS/2 System Editor (e.exe) or EPM and type in the following lines:

/* This is our first Rexx program */
Say "Hello, I'm Rexx. Woof! Woof! Woof!"

Then save it into a file called hello.cmd. Open up an OS/2 command prompt, navigate to the directory where you saved the file, and type "hello" followed by Enter. You should see something like the following on the screen:

[C:\]cd Work
Hello, I'm Rexx. Woof! Woof! Woof!

You've just learned your first Rexx command: Say. Its job is to send messages to the screen (or more precisely, Standard Out (STDOUT), but that's a technicality we'll ignore until later). In Rexx it doesn't matter if you use upper case, lower case or mixed case when you type commands. I could have used "Say", "SAY", "say" or "sAy" and it would have still worked. Use whatever feels comfortable to you.

Important: All Rexx programs must start with a comment block (a phrase encased in the /* and */ characters) on the very first line! This tells the OS/2 interpreter that it's dealing with a Rexx program as opposed to a simple batch file. Try taking out the comment at the top (/* This is our first Rexx program */) and see what happens. OS/2 should display the whole "Say 'Hello..." line and complain that it doesn't recognize "Say". This is because OS/2 is trying to look for a program on your hard drive called "say" and it isn't finding one. Without the comment block at the top, it doesn't realize that it should be interpreting a Rexx program.

You'll also notice that we put the "Hello, I'm Rexx" phrase in double quotes. In Rexx, you can actually use single or double quotes interchangeably. This makes it quite handy if you must use one or the other kind within the string (phrase or sentence) itself. Wrap the string in single quotes if you're going to use double quotes inside (eg: Say 'I turned around and said, "What?"') and double quotes if you're going to use a single quote or apostrophe inside (eg: Say "I'm pleased to meet you.") Rexx will figure out what you mean on a line-by-line basis, so you can mix methods within the same program.

In the next article in this series, I'll start describing how to do simple arithmetic and make decisions in your code. We'll be slowly building up to our first real-world example of a macro that you can use in your applications.

Copyright © 1998 - Falcon Networking ISSN 1203-5696
October 1, 1998