pyperlish

PyPerl Developer's Guide

 

Table of Contents

1. Why PyPerl?

I've used perl for several years, and been very impressed with its ease of use. When you need to do something new, chances are there is an idiom which lets you do it in a few keystrokes. I didn't want to lose that in moving to python. Somehow I wanted to get the benefits of perl's idioms with the robust scalability and maintainability of python.

So the idea is to emulate perl idioms, no matter how we implement the python code under the covers.

See tests (test/basic_tests.py) for those idioms in use.

2. Approach

We make a PyPerl object, so we can protect the "globals" from conflicts if we happen to want several copies running in parallel. Then we establish the foundations:

  • Scalars are ok as is. Not quite perl semantics, but close enough.
  • Array are ok as is. Not quite perl semantics, but close enough.
  • Hashtables are ok as dictionaries.
  • DIR needs its own object, to hide the opendir, readdir, closedir acitons.
  • File needs its own object, to hide redirections and a few other actions.
  • Everything else fits in the PyPerl object.

We need to expand "$" and "@" names in strings. This in turn requires access to the caller's symbol table. "_expand" handles this. Not a perfect rendition, but good enough for the idiom. This is so useful, we export it as the "__call__" function of PyPerl.

File operators (e.g., "-e") turn into functions of the same name. All the functions which take strings (including file operators) need to call the expansion process.

Regular expression functions like "m" need to be rendered as python re actions. I may not have all the variants covered, but enough to take a run at it. In particular, an idiom I was missing in python can now be handled:

  if p.m("he(l+)o","helllo, world"):
      print p("found $1")

After that, it is mostly just going through Programming Perl, implementing functionality as we go.

 
Creator: Harry George
Updated/Created: 2001-12-28