October 18, 2004

The Pain of Object-Oriented Perl

I like Perl. I've used it since sometime in the late 80's. I don't use it everyday, but I use it when I want to munge some text in one way or another.

I recently wanted to generate some RSS feeds for a few websites that didn't provide their own feeds. That meant HTML parsing and RSS (XML generation) which sounded like a job for Perl. I actually considered Ruby and Python, but in both cases I found it difficult to do searches for modules that would do friendly HTML parsing and RSS generation.

So into the land of Perl I went armed with my O'Reilly books.

Having learned Perl in the Old Days before it was objectified, I still have a hard time when I try and use OO-Perl. This project was no different. In fact, I got extremely frustrated.

I had created a class using the Class::Struct package. This is a fairly neat little package that creates setters and getters for you. All you do is define the names of your ivars and their types (scalar, array, class, etc...)

The first bit of ugliness I ran into was when I wanted to create access my ivars. I had an ivar named 'classes' which was an array. An obvious first guess at how to do this would be:

$self->classes

but that would be wrong. Instead you need:
$self->{'classes'}

And, if that's not bad enough, if you actually want to use that expression somewhere in an array context, you need to explicitly state the context by doing this:
push(@{$self->{'classes'}, $foo);

Ugh. The pain. The pain.

I actually got used to (but not happily) the funky ivar access syntax, but I was constantly bitten by my failure to specify the array context syntax.

I did manage to get my projects finished. But, I'm just not happy about doing OO programming in Perl. It still feels clunky and leaves me too many places to do the wrong thing.

Posted by rob at October 18, 2004 10:43 PM
Comments
Post a comment









Remember personal info?