Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

TorgoX (1933)

TorgoX
  sburkeNO@SPAMcpan.org
http://search.cpan.org/~sburke/

"Il est beau comme la retractilité des serres des oiseaux rapaces [...] et surtout, comme la rencontre fortuite sur une table de dissection d'une machine à coudre et d'un parapluie !" -- Lautréamont

Journal of TorgoX (1933)

Sunday September 12, 2004
02:19 AM

"Setting up Templates"

[ #20841 ]
Dear Log,

Some interesting advice from Glenn C. Reid's greatly underappreciated book Thinking in PostScript :

SETTING UP TEMPLATES

A very strong technique for building a structured PostScript program is to create an empty template for each construct in your program and then to go back and fill in the body of the template. This keeps you from having to remember to close all your delimiters and lets you perform your housekeeping all at once, so you can concentrate on other things. Anything that inherently has a beginning and an end will benefit from this technique, including making entries in a dictionary, creating an array, a procedure, a string, a loop, or a conditional.

The advantage to the template approach for ifelse statements, procedure definitions, loops, or other structural components is that you never have a partially written program with mismatched braces, which can be difficult to debug. The procedure body will already "work" when you finish with the template, although it may not do anything very interesting. At least you won't have a break in the structure of your program, even for the time it takes you to finish writing the procedure. Even if you are interrupted in the middle of building your true clause, the braces will still be balanced. It is also highly readable this way.

Example 3.6 shows how to lay out a template for a procedure definition, a dictionary, an array, an ifelse statement, and a loop. Once the template is in place, you can then come back to fill it in. The blank lines in the templates are where you add code later.

Templates aren't just a learning tool, they are sound practice. The very best programmers use this technique every day, whether they are writing a simple example program for a book on PostScript or developing a quick hack to turn off the start page on the printer. Using templates will save you enormous amounts of time in the long run, and it is a highly recommended technique. If you learn nothing else from this book, please develop a habit of laying out templates as you program. Close those curly braces right after you open them, then go back and fill them in. Or, if you hate to use templates, at least develop your own techniques for laying out and maintaining the structure and indentation of the program source.

Example 3.6: Setting Up Templates

% dictionary template:
/mydict 24 dict def
mydict begin

end %mydict

% procedure template:
/newline % - newline -
{ %def
    % this procedure will simulate "newline"
    % on a line printer

} bind def

% conditional template:
currentgray 0.5 gt { %ifelse

}{ %else

} ifelse

% loop template:
0 10 360 { %for

} for

% combination template:
/mydict 24 dict def
mydict begin
    /newline        % - newline -
    { %def
        currentpoint pop 550 gt { %ifelse
        }{ %else
            0 10 360 { %for
            } for
        } ifelse
    } bind def
end

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.