NetRexx as a Scripting Language

Support de cours NetRexx as a Scripting Language, tutoriel & guide de travaux pratiques en pdf.

Learning to program

Console Based Programs
One way that a computer can communicate with a user is to ask questions and then com-pute results based on the answers typed in. In other words, the user has a conversation with the computer. You can easily write a list of NetRexx instructions that will conduct a conversation. We call such a list of instructions a program. The following listing shows a sample NetRexx program. The sample program asks the user to give his name, and then responds to him by name. For instance, if the user types in the name Joe, the reply Hello Joe is displayed. Or else, if the user does not type anything in, the reply Hello stranger is displayed. First, we shall discuss how it works; then you can try it out for yourself.
Briefly, the various pieces of the sample program are:
/* … */ A comment explaining what the program is about. Where Rexx programs
on several platforms must start with a comment, this is not a hard requirement for NetRexx anymore. Still, it is a good idea to start every program with a comment that explains what it does.
An instruction to display Hello! What’ s your name? on the screen.
An instruction to read the response entered from the keyboard and put it into the computer’s memory.
who The name given to the place in memory where the user’s response is put.
if An instruction that asks a question.
who = ” A test to determine if who is empty.
then A direction to execute the instruction that follows, if the tested condition is true.
say An instruction to display Hello stranger on the screen.
else An alternative direction to execute the instruction that follows, if the tested con-dition is not true. Note that in NetRexx , else needs to be on a separate line.
say An instruction to display Hello, followed by whatever is in who on the screen.
The text of your program should be stored on a disk that you have access to with the help of an editor program. On Windows, notepad or (notepad++), jEdit, X2 or SlickEdit are suitable candidates. On Unix based systems, including MacOSX, vim or emacs are plausible editors. If you are on z/VM or z/OS, XEDIT or ISPF/PDF are a given. More about editing NetRexx code in chapter 23.1, Editor Support, on page 63.
When the text of the program is stored in a file, let’s say we called it hello.nrx, and you installed NetRexx as indicated in the NetRexx Quick Beginning Guide, we can run it with nrc -exec hello and this will yield the result:
\nr portable processor, version \nr after3.01, build 1-20120406-1326 Copyright (c) RexxLA, 2011. All rights reserved. Parts Copyright (c) IBM Corporation, 1995,2008.
Program hello.nrx
===== Exec: hello =====
Hello! What’s your name?
If you do not want to see the version and copyright message every time, which would be understandable, then start the program with:
nrc -exec -nologo hello
This is what happened when Fred tried it.
Program hello.nrx
===== Exec: hello =====
Hello! What’s your name?
Fred
Hello Fred
The ask instruction paused, waiting for a reply. Fred typed Fred on the command line and, when he pressed the ENTER key, the ask instruction put the word Fred into the place in the computer’s memory called “who”. The if instruction asked, is “who” equal to nothing:
who = ’’ meaning, is the value of “who” (in this case, Fred) equal to nothing:
”Fred = ’’ This was not true; so, the instruction after then was not executed; but the instruction after else, was.
But when Mike tried it, this happened:
Program hello.nrx
===== Exec: hello =====
Hello! What’s your name?
Hello stranger
Processing of ’hello.nrx’ complete
Mike did not understand that he had to type in his name. Perhaps the program should have made it clearer to him. Anyhow, he just pressed ENTER. The ask instruction put ” (nothing) into the place in the computer’s memory called “who”. The if instruction asked, is:
who = ’’ meaning, is the value of “who” equal to nothing: ’’=’’
In this case, it was true. So, the instruction after then was executed; but the instruction after else was not.

Comments in programs

When you write a program, remember that you will almost certainly want to read it over later (before improving it, for example). Other readers of your program also need to know what the program is for, what kind of input it can handle, what kind of output it produces, and so on. You may also want to write remarks about individual instructions themselves. All these things, words that are to be read by humans but are not to be interpreted, are called comments. To indicate which things are comments, use:
/* to mark the start of a comment
*/ to mark the end of a comment.
The /* causes the translator to stop compiling and interpreting; this starts again only after a */ is found, which may be a few words or several lines later. For example,
/* This is a comment. */
say text /* This is on the same line as the instruction */
/* Comments may occupy more
than one line. */
NetRexx also has line mode comments – those turn a line at a time into a comment. They are composed of two dashes (hyphens, in listings sometimes fused to a typographical em dash – remember that in reality they are two n dashes.
— this is a line comment

Strings

When the translator sees a quote (either ” or ’) it stops interpreting or compiling and just goes along looking for the matching quote. The string of characters inside the quotes is used just as it is. Examples of strings are:
’Hello’
”Final result: ”
If you want to use a quotation mark within a string you should use quotation marks of the other kind to delimit the whole string.
”Don’t panic”
’He said, ”Bother”’
There is another way. Within a string, a pair of quotes (of the same kind as was used to delimit the string) is interpreted as one of that kind.
’Don’’t panic’ (same as ”Don’t panic” )
”He said, ””Bother””” (same as ’He said, ”Bother”’)

Clauses

Your NetRexx program consists of a number of clauses. A clause can be:
1. A keyword instruction that tells the interpreter to do something; for example, say ”the word” In this case, the interpreter will display the word on the user’s screen.
2. An assignment; for example, Message = ’Take care!’
3. A null clause, such as a completely blank line, or ;
4. A method call instruction which invokes a method from a class ’hiawatha’.left(2)

When does a Clause End?

It is sometimes useful to be able to write more than one clause on a line, or to extend a clause over many lines. The rules are:
. Usually, each clause occupies one line.
. If you want to put more than one clause on a line you must use a semicolon (;) to separate the clauses.
. If you want a clause to span more than one line you must put a dash (hyphen) at the end of the line to indicate that the clause continues on the next line. If a line does not end in a dash, a semicolon is implied.
What will you see on the screen when this exec is run?
Listing 2.2: RAH Exec
1 / Example: there are six clauses in this program / say « Everybody cheer! »
2 say « 2 »; say « 4 » ; say « 6 » ; say « 8 » ; say « Who do we »
3 « appreciate? »

Loops

We can go on and write clause after clause in a program source files, but some repetitive actions in which only a small change occurs, are better handled by the loop statement. This always reminds me about an anecdote that Andy Hertzfield tells1:
Bob’s background looked to be a lot stronger in hardware than software, so we were somewhat skeptical about his software expertise, but he claimed to be equally adept at both. His latest project was a rebellious, skunk-works type effort to make a low cost version of the Star called ”Cub” that used an ordinary Intel microprocessor (the 8086), which was heresy to the PARC orthodoxy, who felt that you needed custom, bit-slice processors to get sufficient performance for a Star-type machine. Bob had written much of the software for Cub himself.
”I’ve got lots of software experience”, he declared, ”in fact I’ve personally written over 350,000 lines of code.”
I thought that was pretty impressive, although I wondered how it was calculated. I couldn’t begin to honestly estimate how much code I have written, since there are too many different ways to construe things.
That evening, I went out to dinner with my friend Rich Williams, who started at Apple around the same time that I did. Rich had a great sense of humor. I told him about the interview that I did in the afternoon, and how Bob Belleville claimed to have written over 350,000 lines of code.
”Well, I bet he did”, said Rich, ”but then he discovered loops!”

……..

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *