Main Content

Dont just convert to Perl - re-engineer!

Archive - Originally posted on "The Horse's Mouth" - 2007-10-18 23:37:05 - Graham Ellis

There's a difference between converting a program from one language to another and re-engineering a program to make best use of a new language - and I had a couple of clear examples of that on our Perl for Larger Projects course today.

The following is "converted" code ...

if (! open (FH,"../Desktop/access_log.xyz")) {
print STDERR "We have a weeny problem there!\n";
exit (1);
}


Or "re-engineered" you can write ...

open (FH,"../Desktop/access_log.xyz")
   or die "We have a weeny problem there!\n";


The following is "converted" code again ...

while ($line = <FH>) {
   if (index($line,"cliad") > -1) {
      print "$line";
   }
}


which is much better written as

@info = <FH>;
@matched = grep(/cliad/,@info);
print @matched;


or perhaps even

print (grep(/cliad/,<FH>));

Why re-engineer?
• Performance
• Maintainability
• Readability
• Extendability