I’m not exactly the model programmer, but I’m a big nut on readable code.

I’ve been doing a lot of PHP, but now I’m starting to get back into Perl. And I’m realizing that, in Perl, it’s very easy to write code that makes absolutely no sense to anyone except the most seasoned veterans. It does a lot of what I occasionally chastize myself for doing: taking bizarre shortcuts that shave a few seconds off of writing the code, have no impact on performance, but make your code fairly hard to interpret.

I was trying to figure out how readline() works, for example (I shouldn’t have to look that one up), and they gave this example code block:

    for (;;) {
        undef $!;
        unless (defined( $line = <> )) {
            die $! if $!;
            last; # reached EOF
        # ...

The very first line is odd: a for loop with no parameters. It’s easy enough to assume that it’s to do an infinite loop. The next line ensures that $! is undefined, $! being a variable pointing to errors. (Obviously. What else could $! mean?) The $line = <> is probably clear to anyone who knows Perl, though it’s certainly not intuitive as explicitly stating that it’s referring to STDIN. I find the unless() syntax to be nifty: it’s the same as if(!(…)), but a little clearer. Except I only find it intuitive if it’s expected that the evaluation will be true: unless a rare event happens… Here, it’s unless reading from STDIN is defined.

And even the die() bothers me: maybe there’s no sense in writing out an if($!) { die $! }, but I would have.

All around, I feel as if everything in Perl relies on very terse, bizarre naming schemes, and have always thought that a lot of Perl programmers take pride in writing the most obfuscated code imaginable. There are a lot of languages that I can “read” pretty well: I can sit down and look at your code and figure out what it does. But Perl isn’t one of them. Case in point: I’m scratching my head trying to make readline() read a line of data at a time. That’s what it’s supposed to do, but that’s not what it’s doing. Another time, a function returned an array full of references, a fact that was buried somewhat obscure in the documentation. Don’t get me wrong: it’s a very powerful language, and some brilliant people have done some amazing stuff with it. But it doesn’t have the same beginner-friendliness that other languages do. The same could be said, I suppose, for vi or Linux. But I have lots of reasons why I think Linux and vi are much better than the alternatives. I don’t have any reason to think that Perl is any better than its numerous alternatives. (PHP, Ruby, Python, bash…)

Leave a Reply

Your email address will not be published. Required fields are marked *