Perl oddities

Since Gas is writing about UI, I’ll do programming for now:

Today’s example is about Perl, and a curious example about hashtables. A hashtable, as you may know, maps a key to a value.

%names = ("smith" => "john", "krall" => "diana");
foreach $lastname (keys %names)
{
print "$lastname, $names{$lastname}n";
}

In essence, this hashtable allows you to take a lastname and find a firstname with it. If we look up “smith” in %names, we get “john”; if we look up “krall,” we get “diana”.

The second part of the code–the foreach, takes each key in %names and prints it out. So the output is as follows (the order may be switched):

smith, john
krall, diana

Perl, of course, is not without quirks that make this seemingly simple behavior complicated.

Quick quiz: What does this code do?

%hash_to_the_l = ( "turner",
"baggage" => "smashing",
"cheeseball" => 43, "ducks");
foreach $key (keys %hash_to_the_l)
{
print"key $key = $hash_to_the_l{$key}n";
}


Answer:

key 43 = ducks
key smashing = cheeseball
key turner = baggage

Wondering why? Think about it.

Ad Hack

As we originally established, if you build a hash table out of keys, you can use arrows “=>” to connect keys to values. You could say “celery” => “vegetable”, “granite” => “mineral”, and Perl would understand that you were connecting each key to a value that happened to describe its nature.

What was unique about the last example was that it didn’t just use those x => y pairs. Let’s look at it again:

%hash_to_the_l = ( "turner",
"baggage" => "smashing",
"cheeseball" => 43, "ducks");

This is a lesson in how Perl handles hashtables. The object on the left is really just an list–not a hash–pure and simple:

%hash_to_the_l = ( "turner",
"baggage" , "smashing",
"cheeseball" , 43, "ducks");

The arrows and commas are almost completely interchangeable. Perl is taking every odd element and using it as a key to point to a value. So “turner” points to “baggage”, “smashing” points to “cheeseball”, etc.

Most people will never realize that commas and arrows are the same thing in Perl. They look different, after all; conceptually we understand that keys point to values and not vice versa.

Fearful Programming

This is an example of fearful programming, the state in which you can’t trust that things are correct without looking at the documentation yourself. For better or for worse, this state is often true in scripting languages. For example:

$string1 = "cheese " + "bread";

is not acceptable in PHP, which uses “.” instead of “+” to add strings.

Fearful programming is quite common as far as programming goes. While it may be considered bad or neutral, it stands as a big roadblock in programmer efficiency whenever a coder cannot simply think out what they are doing and produce it. Removing that roadblock can do wonders for productivity.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>