Sasara serving up Perl and C

Perl is one of those languages that defies classification. Or at least, to me. Here we have what’s clearly an interpreted scripting language with all the ease of automatically scaling arrays, but that also has lower level features you’d expect from the likes of C. One such feature is the almighty reference.

The image above is of the lovely Sasara anime figure Clara bought me. She’s wearing an apron, on a Perl Cookbook. That makes sense, right? ^^;

I didn’t even make an Inception Xzibit joke

If you come from C, Objective-C, C++ and the like, you can think of a Perl reference like a pointer. Rather than storing a value, a Perl reference stores a reference (that was inspired!) to a scalar, array, hash and the like.

To store a reference to a scaler, we would do this:

#!/usr/bin/env perl -w

use utf8;
use strict;
use warnings FATAL => 'all';

my $chitanda = "kininarimasu";
my $chitanda_ptr = $chitanda;


Running this code will give us this:


As we would dereference in C, we can print the value of the scalar we’ve referenced by doing this:


Which will return:


Did I just blow your mind? Probably not, but when I first came across that in Perl I squeaked with as much delight as my teeth do when eating a slice of Halloumi. You know, that squeaky cheese? That stuff is dope. But I digress; think of the possibilities of this.

Okay cool, why bother?

I got into a conversation with someone at UTS about Perl references. Their response was a mixture of surprise and laugher, followed by a question as to why you would ever want to use references in what’s traditionally known as a scripting language. He probably didn’t eat Halloumi either.

There are some situations where passing a reference to a variable would be preferable to passing its value. If you’ve developed a complicated or long data structure, it can take time and memory to reproduce. There are also times where its necessary; a potentially confusing limitation for people starting out with Perl is arrays can only contain scalars. Akin to a C array, Perl stores arrays of arrays (or hashes) as references to other arrays. Perl hides this somewhat, but there are times when you’re modifying arrays (or hashes) where knowledge of this can really help.

Array and hash references in Perl are something I’ve recently had to reacquaint myself with in more detail, and will be the subject of a future post. When I’ve written it, I’ll reference it here. Heh.