jump to navigation

Simplifying Perl Variable Types December 25, 2009

Posted by ddouthitt in Perl.
Tags: ,
add a comment

Perl uses punctuation such as $ and @ to specify what type the variable is – scalar, array, hash, etc. When one first learns Perl, you learn:

$a = 4;    # scalar
@a = ( 4, 5, 6 );    # array
%a = ( a => 4, b => 6 );    # hash

But then constructs like @$a (a valid construct: a reference to an array) can become confusing. If you think of these marks as operators instead, then it becomes clearer and simpler. Then $a becomes “treat a as a scalar” and @a becomes “treat a as an array”. Thus @$a is “treat $a (a is a scalar) as an array”.

The book Effective Perl Programming by Joseph Hall et al covers this and is an excellent resource for all Perl programmers. According to the book, there is a web site at http://www.effectiveperl.com but this seems to be in Russian – though it is indeed about Perl. The author, Joseph Hall, has a blog at http://effectiveperl.blogspot.com but it hasn’t been updated since 2007. At least, there’s a second edition of the book coming out soon. This book should be on every Perl programmer’s bookshelf.