Pro's and Con's of default variable $_ in Perl

In this post we will see some good practice while you write your Perl scripts . If you see my previous post Getting started with Awesome Perl I have discussed usage of default variable $_ while what we see in this post how good for a programmer is to use the default variable

It's for sure that the default variable ($_) does the work but when you have a large chunk of code , readability of the code will become a problem . Lets take a piece of code and analyze it .

Let's pass some sql data into an array like one shown below

my @data = `db2 "list tables for schema DB2INST1"`

Now the array @data is populated with some data , lets go and do some manipulations

Without mentioning the default variable $_

foreach (@tbsp){
chomp;
s/^\s*//;
s/\s*$//;
print;
}

This is what it actually looks like

foreach my $_ (@tbsp){
chomp($_);
$_ =~ s/^\s*//;
$_ =~ s/\s*$//;
print $_;
}

As the name implies Perl makes the $_ to take the default value even if you explicitly don't mention the variable to store the value.

Lets see where this default variable $_ will draw us into confusion

#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;

my @data = `db2 "list tables for schema DB2INST1"`;
foreach (@data){
chomp;
s/^\s*//;
s/\s*$//;
print;
}
my @data = `db2set -lr`;
foreach (@data){
chomp;
s/^\s*//;
s/\s*$//;
print;
}
my @data = `db2 "select * from EMP"`;
foreach (@data){
chomp;
s/^\s*//;
s/\s*$//;
print;
}
my @data = `db2 "get dbm cfg"`;
foreach (@data){
chomp;
s/^\s*//;
s/\s*$//;
print $_;
}

If you see I have passed 4 times 4 different types of input to the array @data at mentioned line numbers

my @data = `db2 "list tables for schema DB2INST1"`;
my @data = `db2set -lr`;
my @data = `db2 "select * from EMP"`;
my @data = `db2 "get dbm cfg"`;

For every input $_ is taking the respective array's (@data) data for that particular iteration and processes it in that respective foreach loop . Executing this script would produce all the inputs we have passed to the array @data.

Wow , Its working as it should but that's not our point . Our goal is to write a code that should have a good readability and help us to debug the code at a later time for not only the persom who wrote the script but for anyone who want to deal with it.

So if you see the code above there is a definite confusion of which $_ is handling which data or which input.So we make it better by following some coding standards.

Lazy Coding

foreach (@tbsp){
chomp;
s/^\s*//;
s/\s*$//;
print;
}

Relaible Code

foreach my $tbvar (@data){
chomp($tbvar);
$tbvar =~ s/^\s*//;
$tbvar =~ s/\s*$//;
print $tbvar;
}

Let's take some part of the above script and see how that looks

#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;

my @data = `db2 "list tables for schema DB2INST1"`;
foreach my $tbvar (@data){
chomp($tbvar));
$tbvar =~ s/^\s*//;
$tbvar =~ s/\s*$//;
print $tbvar ;
}
my @data = `db2set -lr`;
foreach my $regvar (@data){
chomp($regvar));
$regvar =~ s/^\s*//;
$regvar =~ s/\s*$//;
print $regvar;

See now the readability of code is perfect , that you understand which variable is carrying which data in which iteration .

These are minute things that can be neglected but there is something called coding standards and for yourself to call a good programmer you need to follow these standards.

Are you a good programmer ?

Have a Nice Day

No comments:

Post a Comment