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.

Advertisements

Debugging: A Perl Pet Peeve December 24, 2009

Posted by ddouthitt in Debugging, Perl, Ruby.
add a comment

When Ruby hit 1.45 years ago, I tried it and fell in love with it right away. The biggest problem with Ruby is the same now as it was then: its not available on every platform; most commercial UNIXes don’t include Ruby.

Thus, I have kept up my Perl skills. However, I found that debugging is not the same – and thus a pet peeve of mine.

You can always introduce print statements – which is what one of the developers of C and UNIX maintains is the only way to go (instead of “debuggers”).

When debugging Ruby, one can do this:

p $a;

When debugging Perl, this is required instead:

use Data::Dumper;

print Dumper($a);

I’m also always confusing Dump() with Dumper() as well.

Why didn’t Perl use a simple method instead of a complicated library process? Who knows?

Even so, Perl has a sophisticated built-in debugger which Ruby does not – but as mentioned before, printing variable contents can be more profitable for debugging purposes.