Named Variables

Beside the integer and string types of operands, there is also a “name”
operand. You can recognize a literal name because it is preceded by a
slash. It can be any word you make up. Since all the built-in
commands in Postscript are lower case, you can’t go wrong by throwing
in an upper-case letter or two. Here are some of the names I made up
for a program we’ll be talking about later:

/pageHeight
/pageWidth
/countDown
/countAcross
/ProcessWhite
/ProcessBlack
/XtractCoords
/MoveFrom
/MoveTo
/DrawMove
/DrawPiece
/DrawBoard

You get the idea.

Literal names are almost always first used with the def operator,
which defines the name as a variable which contains an operand or a
procedure. The Adobe reference manual says

key value def

which means the def operator expects on the stack a key (the name) and
a value (an operand or procedure) that you want the name to be
associated with. The def operator doesn’t leave anything on the stack,
but the result of using it is that your program will know the value of that variable when you use it.

For example, define three variables:

/firstNum 2 def
/secondNum 3 def
/MyProcedure { add } def

Notice that braces go around the add operator to defer it from
executing immediately.

In the program, use the names without the slash prefix in order to
execute them:

firstNum secondNum MyProcedure

is equivalent to saying

2 3 add

The number 5 will be left on the stack to do with as you will, such as
storing it in another variable:

/theResult firstNum secondNum MyProcedure def

You want to increment theResult by two? Do this:

/theResult theResult 2 add def

or even

/bump { 2 add } def
/theResult theResult bump def

By this time the value held by the variable theResult should be
something like 9. How do you find out for sure? In our last lesson we
introduced the “print” operator which is only good for printing
strings. Another built-in procedure is the equal sign,=, which first converts
anything into a string and then prints. Now is a good time to create a
test file, save it as test.ps (or whatever), and run it thru your
interpreter:

/firstNum 2 def
/secondNum 3 def
/MyProcedure { add } def
/theResult firstNum secondNum MyProcedure def
(theResult is ) print theResult =