Thursday, April 10, 2008

It's just a simple IF statement.. isn't it?

Okay here we come again with a weird problem I encountered today using perl. Take the following code:

#!/usr/local/bin/perl

use strict;
use warnings;

use Data::Dumper;

my $myvar;

print Dumper($myvar);

if ($myvar->{bleh}) {
$myvar->{bleh} = 'cool';
}

print Dumper($myvar);
So the value of 'myvar' should stay the same, whatever it is, from the first dump to the second dump right? Wrong!

Since the variable 'myvar' is not initialized, when you use it as a hash reference, Perl thinks this variable is a hash reference, and converts it to a hash reference! Here's the output of the above code:

$VAR1 = undef;
$VAR1 = {};
The value of this variable has clearly changed without notifying the programmer! Remember to always initialize your variables!

For this experiment I was using:

This is perl, v5.8.8 built for x86_64-linux-thread-multi

Damn you post conditional statements!

Consider the following code:



sub my_sub {

my %args = @_;

my $first_param = $args{first} if ($args{first});

print Dumper($first_param);

return 0;

}
So this code might seem all fine and work with no problem. But imagine the following scenario:
my_sub(first=>'hello');
my_sub();
What's the expected behavior? You would think that in the first call you would get a dump of 'hello' and in the second call get a dump of... of? undef? ''? Now this is what you get when you run the code:

$VAR1 = 'hello';
$VAR1 = 'hello';
Explanation:

So when the subroutine is called for the first time, the 'first' argument is defined, and the 'first_param' gets set to its expected value and there are no problems encountered. The 'first_param' variable's scope is just within the subroutine and it should get destroyed and blanked out when the method returns. But when the second time the subroutine is called, since the 'my' keyword is there, the 'first_param' is defined, but since the post conditional if() statement is false, the variable 'first_param' does not get set to undef or blank, instead it gets set to what that value was the last time the method was called!

I'm suspecting that Perl allocates the exact same memory location for the first_param variable the second time, and since it never gets initialized, it'll remain as what it was before!

Have a good day folks!



For this experiment I was using:

This is perl, v5.8.8 built for x86_64-linux-thread-multi

Welcome!

Okay so I've been developing in Perl programming language for quiet some time at work. This language is probably one of my least programming languages but unfortunately it is heavily being used at my work and my group just doesn't have an option of porting the code into some other programming environment. The system has grown huge and some members of my group are hesitant in learning a REAL language. So I (and probably other software engineers here) have to live with it and get the job done.

Every once in a while I spend hours pulling my hair trying to figure out why some code acts funky or behaves in an unexpected manner. So I thought I document these every time I encounter them, this way I can refer back to them every once in a while as well as share it with other unfortunate Perl developers out there!

Please if you know of any of these types of "features" let me know and I'll be happy to post them. Also Perl monks out there, please correct me if I'm wrong in any of my explanations and I'll be more than happy to change the explanation and update the credits for it:)

Happy Coding!