Thursday, March 1, 2007

On Perl Gotcha's again

Two weeks ago I was telling about how I have spebnt half of a day trying to debug some Perl scripts. The problem was related to the fact that in Perl 5.0 was missing the capability of handling long integers.
Today this problem stroke again. But in a more perverted way. Thed scripts were deployed somewhere in Sri Lanka. Suddenly they stopped working and the system was full of alarms of type "Event too old".
I had a quick look and i've seen that the length of the string "1000*time()" became suddenly shorter and it was under the 11 characters needed. WTF? Perl was storing the data internally on 32 bits so the multiplication with 1000 led to an overflow.
So the line "print sprintf("%011x", 1000*time()) was futile.
Perl is quite limited in his scalar data types so I had to make an improvisation.
My solution was:


sub fixTime
{
$n = 1000.0 * time(); #we force a conversion to float
$h = int($n/65536); #equivalent of a 16 bits rshift
$l = int($n - $h*65536.0); #remainder of the number
return sprintf("%07x%04x", $h, $l);
}


Geee...

No comments:

Post a Comment