The T-Files


Mon, 19 Jul 2004

The thousand and one reasons to love Perl: [7] Run-time evaluation

There is a saying in the Perl community that nothing but perl can parse Perl. While this mainly alludes to the two well known facts that the Perl syntax is much more complex than that of other programming languages, and that Perl is very well suited to write parsers, it also hints at particularily useful feature common in interpreted languages: run-time evaluation. This ability to treat input data as little programs makes it very easy to extend and configure your Perl applications.

With Perl, you do not really need, for example, a special syntax for configuration files. If you can live with the obvious security implications, you can just use Perl snippets instead of .ini, .properties or XML files. Using those files from your application is actually easier than reading any other type of file (the built-in do() function does all the work), and you can

  • use comments, spacing and indentation at will
  • use Perl's many quoting styles
  • use complex data structures -- arrays, hashes, nested if required (ever tried to create an array in a Java properties file?)
  • set Perl variables
  • set environment variables
  • load additional modules
  • call functions to calculate some settings dynamically
  • or anything else you can do in a program, but never dared to ask of a configuration file.
One person's data is another person's program.

Programming Pearls, Communications of the ACM, Sept. 1985