I have been coding in Perl for a few months now and below is a list of few things which I find quirky. This is not an attempt at flaming or trolling or “X language is better then Y language” sort of argument. I will keep adding to this list as soon as I come across something new. So, think of this as a growing document.

Perl has a rich set of libraries in the form of CPAN which is a life saver but, as there is no coding standard associated with Perl, all the library APIs have their own naming conventions. For example ThisIsMethod(), this_is_method(), thisIsMethod(), thisismethod(), etc. When you include many libraries in your code, following your own coding standard becomes difficult. Contrast this with Java where all class names begin with Capital letters, method names have camel case etc.

Perl has the concept of default variables to reduce the burden of typing variable names.

my @array = (1, 2 ,3);
print $_ for (@array);

Here $_ is the default variable for for loops which “POINT”s to the current array element. (The emphasis on POINT will become clear when you read ahead). This default variable is provided only if you have not provided your own variable.

my @array = (1, 2 ,3);
for my $num (@array) {
  print $_;
  print $num;
} 

Here $_ has no value.

Also you can declare your own variables with the same name as default variable.

my $_ = 0
my @array = (1, 2 ,3);
for my  (@array) {
  print $_;
}

What do you think $_ inside the loop prints? The default variable $_ inside a loop shadows your declared variable $_. Once you come out of the loop, $_ is back to 0.

In Perl, changing one variable does not change any other variable (leaving references out). But this is not followed in the case of arrays. Check out the code snippet below:

my @array = (1, 2 ,3);
for (@array) {
  $_++;
}

Now your array has (2, 3, 4).

I hope now you are clear with the emphasis on POINTs in the previous paragraph.

Perl has a method called defined to check as to whether a variable is defined or not.

my $var = 0;

Now defined($var) is true.

But

my @array = ();
my %hash = ();

Testing the above for defined results in false.

Advertisements