Perl versus PostScript


This compares the basic programming structures for loops, arrays, associative arrays, branching, local variables, subroutines, require and debugging of Perl and PostScript.

The two languages differ mostly obviously in word order and punctuation. They are similarly (i.e. very) concise. PostScript is stack-based of course; it has no variable scoping, all named variables are global, but the current state of all variables (except strings) can be saved and then restored later.


# Perl loops $i = 1; while () { $i++; if ($i > $n) { last; } } # or foreach (@an_array) { &do_something($_); # the array element is in $_ } % PostScript loops /i 1 def { /i i 1 add def i n gt { exit } if } loop % or an_array { do_something % the array element is on the stack } forall
# Perl arrays @b = (4, 3, 2, 1, 'bang'); $n = scalar @b; # sets n to 5 $a = $b[4]; $b[4] = 'whimper'; foreach $x (@b) { } % PostScript arrays /b [4 3 2 1 (bang)] def /n b length def % sets n to 5 /a b 4 get def b 4 (whimper) put b { /x exch def } forall
# Perl associative arrays %phonenums = ('Fred', '1234 5678', 'Gina', '2345 6789'); $x = $phonenums{'Gina'}; $phonenums{'Harry'} = '3456 7890'; while (($key,$value) = each %phonenums) { &do_something($key,$value); } % Level 2 PostScript dictionaries /phonenums << (Fred) (1234 5678) (Gina) (2345 6789) >> def /x phonenums (Gina) get def phonenums (Harry) (3456 7890) put phonenums { do_something % the key and value are on the stack } forall
# Perl branching if ($i > $n) { } else { } % PostScript branching i n gt { } { } ifelse
# Perl local variables { local ($i,$j) = 1,1; } % PostScript save and restore save % saves current state of all variables (except strings) on the stack /i 1 def /j 1 def restore % but it's Much Faster to work on the stack when you can, and choose % not-conflicting variable names when the stack gets too confusing ...
# Perl subroutines sub do_something { my ($arg1, $arg2, $arg3) = @_; $some_result = 42; return $some_result; } $x = &do_something ($arg1, $arg2, $arg3); % PostScript subroutines /do_something { /arg3 exch def /arg2 exch def /arg1 exch def /some_result 42 def some_result } def /x arg1 arg2 arg3 do_something def % If working the stack and choosing not-conflicting variable names % isn't good enough, then you'll have to save variable state ... /do_something { save 3 cycle % save current variable state (3 because 3 arguments) /arg3 exch def /arg2 exch def /arg1 exch def /some_result 42 def some_result 1 cycle restore % restore variable state } def /x arg1 arg2 arg3 do_something def
# 'requiring' a Perl library require '/home/fred/pl/a_library.pl'; % 'running' a PostScript file (assuming of course there is a filesystem) (/home/fred/ps/a_library.ps) run
# debugging a Perl script warn "my_subroutine: gloop=$gloop gleep=$gleep\n"; % debugging a PostScript file (my_subroutine: gloop=) = gloop = (gleep=) = gleep = flush

References: Programming Perl, Larry Wall, Tom Christiansen and Randall Schwartz, O'Reilly, 1996, and the PostScript Language Reference Manual, Adobe Systems Incorporated, Addison Wesley, 1991.
See also www.faqs.org/faqs/postscript/faq/


Back to P J B Computing or to www.pjb.com.au . . .