Eval in Other Languages

Let’s find out what other languages have by way of run-time evaluation and exception handling.

Tcl (Tool Command Language)

The Tcl interpreter follows the typical shell syntax: each statement is a command followed by a bunch of arguments. If the command is known at compile-time, it generates byte-codes and executes it subsequently, but if it is a variable, the interpreter waits until run-time to compile and execute that statement. (Earlier versions of Tcl always treated the program as strings and parsed a statement every time it was hit, even if it was within a loop. As this book goes to press, the Tcl interpreter has just recently taken some steps toward becoming a byte-code interpreter.) Tcl supports a user-level eval call, which recursively calls the parser and interprets the contents of the string as a command followed by a bunch of parameters.

For error handling, Tcl provides the error and catch statements, equivalent to die and eval in Perl.

Python

Python’s eval function allows a string to be evaluated and executed, but the string cannot contain newlines. An exec statement allows newlines, but since Python relies on leading whitespace instead of an explicit block structure, it is important that you get the whitespace correct in a dynamically constructed string to be given to exec. This is quite a bit more painful than getting the block scoping right in Perl.

Python goes through a compilation and execution stage similar to Perl, and for every module called ...

Get Advanced Perl Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.