obfuscated perl

(written while working at a large pharmaceutical company)

I’ve been “working” most of the day, but it’s what I call
“obsession work” — like obsessively scratching an itch. I doubt my
boss understands why I work so hard on things like this.

I was handed some legacy Perl stuff to patch up. When I say legacy, I
mean perl4, and when I say patch up — I have never, ever seen
code this bad.
I mean — I’ve seen lots of code, and I’ve
seen lots of poorly written code with lots of bugs, but this
is in a class of its own — because, you see, it
works
. It’s like Wolfgang Pauli said –
“This isn’t right. This isn’t even WRONG.” The guy
who wrote this stuff essentially takes several thousand lines to do
what I can do in fifty, because he reinvents
everything
in the process.

Here’s a representative sample; bear in mind that he does
many things just like this, all just as horrible.

He doesn’t know about arrays. He doesn’t know about arrays!
Instead, he has two scalar (string) variables: mydata, a
concatentation of all the data values, and locations, a
concatenated list of offsets into mydata. For example, take this array:

@apples = ("rome","mcintosh","granny smith","delicious")

He instead does this:


$apples = "romemcintoshgranny smithdelicious"
$apples_offsets = "000004012024"

Get it? Three digits each. The offset of rome is
000, mcintosh 004, granny smith
012
, delicious 024. He knows that the offsets of
the $apples_offsets variable itself (which, after all, is
itself a pseudoarray) are consecutive multiples of 3, so he doesn’t
need a meta-meta-meta-meta-etc-offset.

The mind boggles.

He then goes on to implement, poorly, useful methods like push and
pop. Along the way he “invents” file locking, persistent storage (he
writes his pseudoarrays out to files), and a host of other trivial
things. If you look at it from his perspective, he must think
he’s a genius
— I mean, sheesh: he singlehandedly
invented the concept, or at least the implementation of, arrays! Oh,
and it gets better — in one place he opens a file, reads a filename
in from the file, assigns the filename to a variable, passes the
variable in a web page in javascript, then gets the variable value
back under a different name via CGI, assigns it to another
variable, and finally actually opens the named file.

I see this code and I can’t help but fix it. It’s like …
here’s this immensely ugly thing, and I can tear it all down and make
it beautiful. Never mind that the end product is rather humdrum — a
very simple records entry utility — I’ll sit here for hours studying
his code, looking for all the idiosyncrasies that other systems now
depend on (once others have coded a workaround to a bug, the removal
of the bug would be a bug…), and in the end replacing his
12,000 lines of code with around 200.