Tuesday, February 12, 2013

Perl Poetry

I think Perl poetry is pretty cool. I should write some sometime. I've made one or two attempts, but being the lazy programmer I am, instead of actually finishing any Perl poems, I wrote a Perl module that facilitates the writing of Perl poetry. The module is based on one observation: plain English text, especially the kind you would find in a poem or a novel, is often very close to being syntactically valid Perl. In fact, were you to declare a Perl operator (that is, a subroutine whose arguments don't need to be wrapped in parentheses) for each word that is not already a Perl keyword in some English text, the text would only need small changes to become a syntactically valid Perl script that executes without warnings or errors. The Perl module I wrote does just that: if you use it in a Perl script, it pre-reads the script and declares operators for each bareword that is not a keyword.
To show how it works, here's an example. This is not a valid Perl script:
As I walked through the wilderness of this world, I lighted on a certain place where was a den, and laid me down in that place to sleep; and as I slept, I dreamed a dream. I dreamed, and behold, I saw a man clothed with rags, standing in a certain place, with his face from his own house, a book in his hand, and a great burden upon his back.  I looked and saw him open the book, and read therein; and as he read, he wept and trembled; and not being able longer to contain, he brake out with a lamentable cry, saying, "What shall I do?" 
This is a valid Perl script:
use perlify;

As I walked through the wilderness of this world, I lighted on a certain place where was a den, and laid me down in that place to_sleep; and_as I slept, I dreamed a dream. I dreamed, and behold, I saw a man clothed with rags, standing in a certain place, with his face from his own house, a book in his hand, and a great burden upon his back.  I looked and saw him open_the book, and read_therein; and_as he_read, he wept and trembled; and_not being able longer to contain, he brake out with a lamentable cry, saying, "What shall I do?"
 They are not very different. To convert the text to a working Perl script, I just used perlify.pm and changed some spaces to underscores to prevent the text from calling certain Perl operators without the correct number of arguments. The default action for a word in this text is to print itself and then return 1. The output of the script is
world den a was where place certain a on lighted I this of wilderness the through walked I As to_sleep place that in down me laid slept dream dreamed I a dreamed I I and_as behold rags place house hand his in book a own his from face his with certain a in standing with clothed man a saw I back looked I his upon burden great a book open_the him saw read_therein he_read wept he and_as trembled contain cry saying lamentable a with out brake he to longer able being and_not
 This example isn't especially creative, but there are ways to embellish it to make it more interesting.

One more thing: if you read perlify.pm, notice a variable named "our $callback".  If you care enough to use the module, I probably don't need to tell you what to do with it.