?

Log in

No account? Create an account

Previous Entry | Next Entry

NB I'm ranting

Do not structure your Perl like this (all comments are mine):

# Initialise a bunch of variables to contain the TEXT of some regexps we'll use
my $RLENGTH = '^.{627}\n$';
my $CONTENT = '^.{80}.{546}\r\n$';
# etc...

# some time later...

sub readLine {
   my ($self, $context, $line) = @_;
   $_ = $line; # why? just to make the following line prettier...
   /$RLENGTH/o ? /$CONTENT/o ? /$TREATMT/o ? /$FSTRING/o ? /$CAMPID/o ? /$EPIFANY/o ? 
   do {
      # stuff that needs to be done for this line...
   }
   # various lines like the two below...
    : print STDERR "Error Line: ", $context->getRecordNumber(), ". Invalid characters found in Email.\n"
   : print STDERR "Error Line: ", $context->getRecordNumber(), ". Bad record length.\n";
}

What a pile of dross. How many errors are there here? Admire the genius of putting the definitions of the regexps used nowhere near the code that uses them, and of having the error messages listed in the opposite order to the regexps.

What was wrong with:

sub readLine {
  my($self,$context,$line) = @_;

  eval {
    die "Bad record length\n" unless (length($line) == 628);
    die "Invalid characters found in email address\n" unless (/.{80}.{546}\r\n/);
        # this regexp is still brain-dead, though
    # stuff that needs to be done for this line...
  };
  if ($@) {
     die "Error line: ".$context->getRecordNumber().": $@\n";
  }
}

Yes, inexperience plays a part. But to get something so grotesquely fucked-up takes effort.

Comments

( 3 comments — Leave a comment )
kilinrax
Jan. 21st, 2004 09:55 am (UTC)
Job security?
araqnid
Jan. 21st, 2004 09:57 am (UTC)
It wasn't impenetrable enough to provide job security, just annoying :|
kilinrax
Jan. 21st, 2004 10:17 am (UTC)
At least it was all written in Perl.

I've seen shell scripts that have:

  • ssh'd into another machines

  • run a shell script on that machine

  • that shell script has invoked dbaccess

  • the psv output of which has been piped into another shell script, which:

    • invoked a perl script to remove javascripts from the output, with regexes

    • invoked a perl script to remove HTML, with regexes

    • invoked a perl script to remove rows which for some reason had an excess of pipes (normally because one of the above, strangely, didn't do what it was intended to)


  • written what remained to a file


Oh, and there was a wrapper to *that* script which invoked it for several sql queries, in sequence.
( 3 comments — Leave a comment )

Profile

araqnid
Steve Haslam

Latest Month

March 2009
S M T W T F S
1234567
891011121314
15161718192021
22232425262728
293031    

Page Summary

Powered by LiveJournal.com
Designed by Tiffany Chow