Porting Perl

Perl is one of the great languages, measured in utility and in influence on other languages. Language snobs can argue about the fine details, but there’s no denying that most languages going forward will have facilities for regular expression parsing and extraction, and these will most probably be compatible with Perl.

Inevitably one wants to port some useful bit of Perl to (insert favorite language here). I needed to do this for some of the web site software. It’s tedious and error prone work. One of the main difficulties is translating Perl regular expressions. You might have a Perl compatible regular expression library for your language, but it probably uses native string literals for the regular expressions, not Perl string literals.

Perl has unusual conventions for writing regular expressions. For example, they can be delimited by practically any character:

s/foo/bar/

or

s!foo!bar!

The point is that if you want to match the character ‘/’, you can use a different delimiter, and then you don’t have to escape the ‘/’.

Perl also has a way to ignore whitespace in patterns, the x option:

s/hello
  world//x

will delete an occurrence of “helloworld”. This is useful for complicated patterns (you can also use comments in the pattern), but it also means that if you want to match a newline (\n) in the pattern, you’ve got to be very careful in your translation of the pattern to your favorite language.

These issues and a bunch of others convinced me that it would be worth writing a little tool to help converting Perl to Cyclone. It’s very incomplete (only translates s/// and m// and qr//) but for some Perl programs (coincidentally, the ones I wanted to translate) it works great.

Check it out in otherlibs/pcre/perl2cyclone.pl.

14 April 2006 by trevor #