Going to Perl School

[This is a repost of what I wrote over here ]

I first used Perl in 2002 - about 10 years ago.  Over the years, I have continued to dabble in it but often felt that stuff like Mason and mod_perl were a little beyond what I needed to achieve.  So I stuck with a Perl/CGI format when writing web pages.  A recent post persuaded me to move forward from there - that is a work in progress.

So, after 10 years, why go to Modern Perl for Non-Perl Programmers held by Dave Cross @ Perl School

(I  will confess I was a little concerned I'd be taking a place that could be used by someone else but I am glad I went.)

Dave started with a walk through the basics of Perl, sigils, arrays, hashes and lists.  Most of this I had encountered before, but, back when I just started Perl, I got confused between the use of (), [] and {} and what a list actually was.  As a result, I ended up with some fairly interesting results trying to define hashes, arrays and access the elements and key value pairs within.

I came from a C programming background; web and mail server work so I was used to strongly typed languages.  With some languages you can have really mad hard typing; you can declare two integers on two separate lines and then get the compiler to consider them of different types - even though they are both integers.  Before that I'd been in engineering software, FORTRAN mostly but some C and even BBC Basic.

So Perl is a loosely typed language, identifying variables more by some form of structure than by type.  Fine.  Having Perl categorised as a loosely typed language really helped me feel Perl was a little more solid.

Solid is useful.

Then we got onto arrays and hashes, neither of which I had misunderstood, although, back in 2002 it was the first time I had encountered associative arrays - but I'm glad the Perl community has decided to call them hashes. 

Not least, because hash is a lot easier and quicker to type than associative array.  4 chars as opposed to 16.  (Another Perl habit - less typing == good.)

So .. hashes and arrays.  Originally, I had a hard time remembering where to use the (), [], and {} with hashes and arrays.  One part, accessing an array element or a hash key value pair was easy - $arr[$index] for arrays and $hash{key} for hashes.

The next part, defining hashes and arrays wasn't so easy and in my early years I used to use () to define an array and {} to define a hash.  Then I would have problems accessing hash key value pairs and never quite understood why.


#!/usr/bin/env perl

use Modern::Perl 2011;
use autodie;

my %hash = { fred => 'wilma',
          barney => 'george'
say %hash;
say $hash->{barney};
gives these errors

Global symbol "$hash" requires explicit package name at ./test.pl line 11.
Execution of ./test.pl aborted due to compilation errors.
Which helps a lot.  As a beginner I rarely understood my mistake.

say $hash->{barney};
say $hash{barney};
today gives

Reference found where even-sized list expected at ./test.pl line 6.
Use of uninitialized value $hash{"HASH(0x111d998)"} in say at ./test.pl line 10.
Use of uninitialized value in say at ./test.pl line 11.
which is an improvement (of sorts!) in that the program doesn't completely die - it just declares a warning about $hash{..}.

There are two solutions to this error - declaring a hash ref or properly declaring a hash.

I tended to use the former

my $hash = { fred => 'wilma',
          barney => 'george'
and then of course $hash->{barney} would work.  And the dereferencing of hashes of hashes got to be fun. 

I finally drummed it into myself, via copious re-readings to the Camel book, that hashes could be defined via %hash = (...); but it took some time to drum that in. 

As a consequence, I felt I didn't quite hook into the language.

The bit that made my day at Perl School is really, really quite simple.

Both array and hash definitions are list assignments. 

So simple, and so clear.  And worth it; really worth it. 

I felt like I had finally landed on Planet Perl. 
(Probably a bit of an exaggeration but on solid ground at last!)


I'm not surprise at your problems. Perl only makes sense when you have the right frame of mind. For example, did you know Perl has over 9 uses for the braces, "{}"? Learning and then keeping these uses distinct is not an easy task. :(

I'd reckon that {} is equally used for multiple purposes in many other languages. For example it is very commonly used to delimit blocks as well as construct hash literals in most C-based languages.

Leave a comment

About lesleyb

user-pic I'm just another crazy individual wandering around trying to figure stuff out.