September 2013 Archives

Is This a Hashref Which I See Before Me? (no, wait, it's an arrayref ...)

As you may know, Perl 5.14 (and up) will allow you to use references for container functions (by which they mean functions such as push or keys that operate directly on arrays or hashes).  I’d never thought much about this feature, except to think that it was, in general, a nice way to save three keystrokes on constructs such as (to steal an example from perldelta):

keys %{$hoh->{genres}{artists}}
Now, saving three keystrokes doesn’t mean much if it makes your code harder to read.  But, in this case
keys $hoh->{genres}{artists}
is actually easier to read, so I call that a win-win.

This feature came up at work recently, and a couple of people whose opinions I greatly respect disparaged its use.  Curious, I asked why.  They pointed me at three blog posts that came out at the time the feature was first announced: two from chromatic, and one from brian d foy (again, people whose opinions I greatly respect).  I hadn’t noticed these (or, if I had, I’d forgotten them), so I took some time to read through the articles and their comments.

However, at the end of it all, despite strong agreement amongst at least 5 fellow programmers whose opinions, as I say, I give strong weight to, I’m still going to disagree with them here.  I don’t see anything wrong with the auto-dereferencing, or at least not enough wrong to warrant a policy of full-stop banning it from my code.

About Buddy Burden

user-pic 7 years in California, 18 years in Perl, 27 years in computers, 47 years in bare feet.