Archive

Archive for February, 2009

Seminar on Open Source Search Tools

February 25th, 2009 No comments

I was the speaker at the Ottawa LAMP meetup for February. I must confess that I really enjoyed giving this presentation as it touch on three of my passions, (1) open source tools, (2) search theory and technology and (3) using technology to help people and organizations work more effectively and efficiently.

The video of my presentation, titled ‘Scalable techniques for large-text repositories: Google in a box with LAMP tools’ is available at http://vimeo.com/3228028.

Special thanks to Andrew Ross for recording such a high quality video.

Perl comparison operators: A cautionary tale

February 19th, 2009 4 comments

Individuals who work with strongly types languages are sometimes surprised by the comparison operators in Perl.¬†¬† Perl actually has two sets of comparison operators – one for comparing numeric values and one for comparing string (ascii) values. Since comparison operators are typically used to control logical program flow and make important decisions, using the wrong operator for the value you are testing can lead to bizarre errors and hours of debugging if you’re not careful.

The simplest and probably most used comparison operators test to see if one value is equal to another value. If the values are equal, the test returns true and if the values are not equal, the test returns false. For testing the equality of two numeric values, we use the comparison operator ==. For testing the equality of two string values, we use the comparison operator eq (EQual).

if (2 == 2) { print “== for numeric values\n”; }
if (‘moe’ eq ‘moe’) { print “eq (EQual) for string values\n”; }

Testing for the opposite, not equal, is very similar. Remember that this test will return true if the values tested are not equal to each other. To see if two numeric values are not equal to each other, we use the comparison operator !=. To see if two string values are not equal to each other, we use the comparison operator ne (Not Equal).

if (5 != 6) { print “!= for numeric values\n”; }
if (‘apples’ ne ‘oranges) { print “ne (Not Equal) for string values\n”; }

Bad things happen when you use a variable (containing a number) for output at some point, and also compare the contained number within to predined flows to support program flow decision. For example:

$number = 5;
if($number == 5){
print “the number is $number\n”;
}

The comparison will fail in many cases (when Perl assumes that the $number contains a string).

Edit: It will not fail in this instance, but knowing that this can happen can help you resolve a very tricky bug.

To protect yourself  from this type of failure, it makes sense to explicity inform Perl that $number contains a number. The way you do this is by assigning floating point numbers to the variable, and carrying out all comparison with floating point numbers. Consider the following example:

$number = 5.5;
if($number == 5.5){
print “the number is $number\n”;
}

Tags: ,