Tuesday, August 29, 2006

My First Decent PERL (-made) Poem

Finally, after months battling with it, I've managed to get good enough with PERL (still not very good) to create one of the programs I've wanted to make for some time. The poem below is the first 3 pages of what was 600-odd pages of output. I'll briefly try to explain the program. You can skip this and go straight to the poem if you want. The poem was made using an interview with Brian Kim Stefans called potentially suitable for running in a loop, Gertrude Stein's Tender Buttons, Wittgenstein's Tractatus Logico-Philosophicus, Wilkie Collins' The Woman in White, and Brontë's Jane Eyre.

The PERL script opens 5 plain text (.txt) files behind the scenes. Once open, it works out how many paragraphs there are in each, and works out the average number of paragraphs across the 5 files. The 6 values (number of paragraphs in each and the average) are noted by the code (this becomes important later).

In sequence, from text file #1 to #5, the following happens:

  • A random integer is chosen from 0 to whatever the number of paragraphs is present in that file, minus 1. So, for example, if a file contains 120 paragraphs, the random function will choose one number between 0 and 119. This is the allocated paragraph from which to source text. If 0 is chosen, I believe this results in a line break or two.
  • A random number is chosen, stating how many characters into this paragraph to begin sourcing the text. If the sentence were to begin "Once upon a time" and the number generated was 7, the output would begin "pon a time". The limit of this random number is assigned by me in the code as a variable, so can be changed easily. I think that the greater the number, the less likely to begin at the beginning of the paragraph.
  • In a similar way to above, the selection length, in characters, is chosen at random, again with a limit set by me. These limits for the random values are constant across the 5 files, although the random numbers themselves may differ from file to file.
  • Now that the paragraph number, selection start number and selection length have been set, these are applied to the files.
  • Each chunk of text is written to a separate output file.
  • Now that the file has been written into 5 times - once from each source file - the process is repeated until each writing of the 5 sources has been completed the same number of times as there are average paragraphs across the 5 files.
  • Phew!
I am in the process of writing through the results. Reading up on Brian Kim Stefans' thoughts on Computer Poems has really helped here in terms of conceptualising how much or little I do with the output, which varies from complete nonsense to full sentences. I cleaned up minimally the text I looked at. Some entire lines of text were deleted as seemed appropriate. I wanted the poem to hint at an air of subjective creation, whilst at the same time displacing the certainty of the language's purpose, keeping it very obvious that this was produced through algorithm. Making the poem as sensical or 'clean' as possible was therefore not my objective, and indeed sometimes I worked in opposition to this idea.

So here it is:


not a body of

as the last
cake shaved into fragmen

Bessie would rather have stayed, bu

e totality of exis
hearsay evidence.
Yes, exactly --

(i.e. we should have to be able t
and yet I know

of elementary proposition
Italy for political reasons (
your roo

together. There is
0251 Space, time, colour (being

"But you comprehend me?" he

a kind.
of log

thing about the animated De Camp
away. Not giving it a

; of that I was sure.


aim of the book is
a moment;
We know from your

from a false proposition.

making the persons
couldn’t perform the Ballet Mécan
we, the weary pilgrims of
oak; the staircase window was high

no more, it shadows the sta

had some

now have to answer a priori the
active habits; such is

coat i
chosen. But God sees not as man

glance over a world quivering

My spirit,"
was what I was trying to d

termined by the negated proposition. Th
ankle is sprained."


one else
ring the past year I had not managed
led under his feet: taking a f


(Negation reverses


the moonbeams; I heard onl
this sense that concrete

he schemata
yself from

in and not less noisy
Proof in logic
lf from the book which
had some extrem

purse; and the story is l


to spend with my mother
an instant; it began again, louder:

A table was
equality have the same

physically speak
interesting, never
consider a lecture, to consider it

assistance only
sir, she sees you!"

reside in the symbol

, 'Do this,' and it

is called bake and
A formal concept
survivors of a family of five

e world.


Blogger James said...

Good work ! Perl is an excellent language for screwing around with text generally, incredibly powerful and adaptable. Like the choice of source texts too !

Hope you're commenting your code properly ;) It's quite easy to lose your mind when debugging perl scripts, especially if you come back to them after a while :)

Have you mucked around with regular expressions at all ? That used to drive me nuts ...

4:33 am  
Blogger textosterone said...

Hey James

Glad to see you're still well, and congratulations on Kate and Tim's engagement, and your best man role.

Yeah, it's strange, because I've been trying to get my head around the basics of PERL for months, and kept hitting a wall with it. Then, suddenly, it came together.

Before I try to get into the next level, I'm creating a series of poems where a poem I've written is passed (parsed) through a PERL script which is itself a poem as code. The result will be a third poem, a mash-up of the first, in an output file.

Unsurprisingly, getting one of these poems done is taking a LONG time, as I'm certainly not up to the stage where I can rattle off code quickly and without often needing to refer to some help. But I've almost done my first one.

I guess PERL is ideal for this as variables can nest themselves within variables, and each variable can have its own name. I'll post up the three texts when I've finished.

Re commenting: I learned this mistake with actionscript. These days I leave myself comments! Especially since I'm going back on code at the moment which contains a load of numerical expressions, which I'm trying to replace with $variables. I've had to comment which line refers to which section of output, so I can be sure it's working and not garbling my output file as I go.

$james = "nerd";

5:46 pm  
Blogger textosterone said...

Or perhaps better practice would have been

$james = 'nerd';

5:47 pm  

Post a Comment

<< Home