Home > Uncategorized > Perl


March 27th, 2010 subogero

I love Perl. The beautiful friendship started blossoming recently. I remember a few years ago, before I got infected with Unix, I was always scared to death when confronted with Perl. But after half a year of meddling with Bash, GNU coreutils and regular expressions, I happened to look into an occasional Camel-smelling script and, much to my surprise, I was literally shocked by its beauty. Let me list my personal highlights.

Perl is like human languages.

open(FILE, "< $filename") or die "$filename could not be opened for reading.";

In Perl, there are many ways to express yourself to stress different things, like in human languages. Not like in German.

if ($foo) { print "Foo!"; }
print "Foo!" if ($foo);
print "Foo!" unless (!$foo);

Perl is very terse, like good slang.

Ever heard of Perl Golf?

Perl comes with batteries included.

With everything from Bash and coreutils, without using any extra modules. In the little Hungarian calendar below, I open a filehandle for a pipe from another program, I use the command line arguments, I do string substitutions.

open CAL, "cal -m @ARGV |";
while (<CAL>) {
  s/January/ Január/;
  # translations for other months and days ...
close CAL;

I forget, where did I hear this “batteries included” last time?

Default variable $_, especially when it’s not even written. Just like ogc (or vice versa).

Look at the code above. Guess what the s/// substitution is working on? Guess what is printed? Exactly. The hidden $_ variable, the result of the previous operation.

Variable substitution within regular expressions.

In my first ever Perl-script I came across some substitution problem where a part of the pattern to change was stored in a variable. I thought: How nice it would be to be able to put the variable into the regexp. I googled it. Of course it works.

$foo =~ s/(^.* )$variable$/$1$bar/;

And don’t tell me it’s unreadable…

Perl is available for Windows.

At the slightly brain-damaged company I work for, I can write small programs for slightly brain-damaged users. They don’t have to install Cygwin to run them, but Perl gives me the full arsenal of Unix.

The Perl style guide.

Compactness. Readability. No bullshit.

The Camel book.

I keep laughing out loud reading it.

Larry Wall is a Bible guy.

Remember, Perl comes from a linguist who originally wanted to translate the Bible into exotic languages.

Larry Wall is always right.

Even when he is not.

Categories: Uncategorized Tags: , ,
Comments are closed.