Perl Is Not Lisp
I've been collaborating with a couple friends of mine on a DSL for a project that is being implemented in Perl. I won't get into what the DSL is for and it doesn't matter for this snippet. We tried an experiment to see how we would go about writing the interpreter for the DSL which is intended to be a stand alone language rather than an embedded language. Our backgrounds are quite different. One guy is a Java developer who knows Perl. The other is a Perl developer. I haven't touched Perl in a few years and have been playing with Lisp recently.
Naturally we came up with three different implementations. The only thing in common was the use of a hash table for language function names. However, my version was quite a bit more different from the other two. If you are familiar with the SICP course, you might find my _eval function eerily similar to the one Gerald Jay Sussman scrawled across four black boards. This is no accident. Although I am missing handling for special forms.
sub _eval {
my @exprs = @_;
my @ret = ();
my $eval; # because perl is stupid
my $next_expr = sub { shift @exprs; };
my $apply = sub {
my $pproc = shift;
my $proc = _pproc_proc($pproc);
my $argc = _pproc_argc($pproc);
my @args = ();
my $ret = _make_int(0);
if ($argc == 0) {
$ret = &{$proc}();
} else {
for my $not_used (1 .. $argc) {
push @args, &{$eval}();
}
$ret = &{$proc}(@args);
}
$ret;
};
$eval = sub {
my $expr = &{$next_expr}();
my $ret;
if (_symbol_p ($expr)) {
my $sym_val = _symbol_value ($expr);
if (_pproc_p ($sym_val)) {
$ret = &{$apply}($sym_val);
} else {
$ret = $sym_val;
}
} else {
$ret = $expr; # self evaluating
}
$ret;
};
while (@exprs) {
push @ret, &{$eval}();
}
@ret;
}
Like Lisp, Perl has higher order functions (map), Lambda expressions, and even lexical closures. Perl just happens to have rather butt ugly syntax. Larry Wall has said that Lisp looks like oatmeal with toenail clippings. Well, as much as Larry may hate Lisp, Perl probably comes closer to Lisp than just about any other language that features the famous curly brackets (outside of Lisp's FORMAT function).
The Perl programmer was not wild about my code.