Why Perl is not my favorite language

August 3, 2005

I could talk about aesthetics; I could talk about line noise and readability; but for me it ultimately comes down to one thing: data structures.

Namely, I find it very hard to write perl code of any depth without running headlong into the fact that Perl's 'collection' types (arrays and hashes) can't contain themselves. All they can contain is scalars (strings, numbers, and references).

For example, recently I was writing a program to mass-query the SBL DNS blocklist and wanted to stick a caching layer in so that I'd only look up a given IP address once. The natural way to do that is with a hash, indexed by the IP address. Except that an IP address can be in more than one SBL record, so the natural representation for the result is an array, which can't be put in the hash.

Perl fans will retort that I can just use references. I could write a long answer, but I'll just go with the short one: 'if I wanted to use pointers, I'd write C'.

That's what references are: sticky pointers. And like all forms of pointers, they're an implementation detail. Low level languages like C are all about the implementation details, but I dislike high-level languages that make me think about them very often. (Implementation details are a distraction; every bit of effort you have to spend on them is effort you are not spending on the real problem.)

I could write more than small things in Perl. It's not that the language is incapable. It's just that it's annoyingly distracting, and I'd generally rather not bother if I have the choice.


Comments on this page:

From 128.117.90.121 at 2005-08-05 13:25:52:

But you don't have to worry about the details. You can just write $fred[53]{'george'}[2]="moo" and it will work.

But I've told you this before and you didn't care then, so I assume you won't care now. Stick with Python, we Perlies don't need ya. :)

-- Squiddhartha

By cks at 2005-08-05 13:59:09:

I think I'm missing something in Perl, then. Here's about the simplest example of the sort of thing I want to do:

#!/usr/bin/perl
use strict;

my %store;
for my $i (@ARGV) {
  if (!$store{$i}) {
     my @array = [$i, "abc-".$i];
     $store{$i} = @array;
  }
  print join("\n", $store{$i}), "\n";
}

This doesn't work to cache and then retrieve intact @array; when it's put into the $store hash, it gets crushed down to a scalar. While I can store the values into $store with $store{$i}[0] and so on, the join line doesn't work as is, because what is stored in $store{$i} is actually a reference to an anonymous array.

Is there some other transparent way to do this in Perl that I'm not aware of?

From 128.117.90.121 at 2005-08-05 14:28:02:

Well, that depends on what you mean by "transparent." True, $store{$i} doesn't just hand you back an array, the way you want. But if you store the anonymous arrays as $store{$i} = [$i, "abc-".$i]; (omitting the intermediate array), and then use join("\n", @{$store{$i}}), it gives the desired result. I admit, it's much more cumbersome to hand entire aggregations around than to deal with individual elements, so you got me there.

-- Squiddhartha

From 78.35.25.22 at 2012-02-23 02:43:18:

Am I missing something? Are you really just asking for this?

#!/usr/bin/perl
use strict;

my %store;
for my $i (@ARGV) {
  if (!$store{$i}) {
     my @array = ($i, "abc-".$i);
     $store{$i} = \@array;
  }
  print join("\n", @{$store{$i}}), "\n";
}

I.e. all you were missing is how to add a dereference.

FWIW, that conditional takes a lot of room to express a small intent and can be written much more to the point:

$store{$i} ||= [$i, "abc-".$i];

Aristotle Pagaltzis

By cks at 2012-03-26 16:26:45:

To be honest I can't entirely reconstruct what I was thinking in 2005, but I think that I considered references annoying (per the paragraph about them in the original entry). Well, not so much references as having to explicitly manage and dereference them.

I also don't know if you can have a reference to a reference; depending on exactly what happens if you try to get one, there are potential non-transparency issues (where your code may need to know whether or not it has been handed a reference or a plain object). I suppose the Perl way around this is to always use references everywhere.

From 78.35.25.22 at 2012-03-26 18:30:46:

I think that I considered references annoying (per the paragraph about them in the original entry). Well, not so much references as having to explicitly manage and dereference them.

I’ve heard that from others. The reverse in Python is that it makes me do extra work every time I want to collapse lists, which I find really irritating. That is really the qualitative difference between the models – whether lists collapse by default and you have to express your desire to maintain their individuality (Perl) or they have a container by default and you have you to express your desire to concatenate them (Python).

I also don’t know if you can have a reference to a reference

You can take a reference to anything, that always works, no matter what it is.

depending on exactly what happens if you try to get one, there are potential non-transparency issues (where your code may need to know whether or not it has been handed a reference or a plain object).

Here I am not sure what that even means in Perl, because there is no way to have an object without a reference. Objects are blessed references in Perl. (Blessing turns a reference into an object by attaching a namespace to the referent, through which method calls can then be resolved.)

I suppose the Perl way around this is to always use references everywhere.

Well, you cannot pass a hash or array without flattening it – unless you take a reference. And objects by definition require references. So the answer to what you probably mean here is that you can’t even not use references.

Aristotle Pagaltzis

Written on 03 August 2005.
« Multilevel list comprehensions in Python
Keep your hands off my font size »

Page tools: View Source, View Normal, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Wed Aug 3 01:39:22 2005
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.