PHP-ers writing perl
1 minute read

Today, I had the dubious pleasure of hacking a bit of perl which clearly wasn’t written by a Perl coder, but rather somebody who learned “programming” by hacking together PHP or something similar.

Take this little piece of code:

print "<DIV CLASS=\"caption\">";
my $tmp = &htmlize_caption ($info{$pathname}{'comment'}, 'slide');
print $tmp;
print "</DIV>\n";
  • unecessary temporary variable
  • ugly “-escaping
  • three print statements and one assignment rather than a single print statement.

This code should rather read:

print qq[<DIV CLASS="caption">],
      htmlize_caption($info{$pathname}{'comment'}, 'slide'),


printf qq[<DIV CLASS="caption">%s</DIV>], 
          htmlize_caption($info{$pathname}{'comment'}, 'slide');

(Really, it should be using templates, but if you aren’t using templates, well, then you aren’t.)

The code doesn’t use strict, but it uses my, it uses long, long, long sequences of print instead of chaining them or templating.

If you are going to write perl, please learn the language properly rather than writing huge scripts which are hard to fix and maintain.

Back to posts