leah blogs

July 2004

15jul2004 · The most awful Perl script

I’ve seen the most awful Perl script ever. I guess it’s the first Perl the author ever wrote. It’s purpose is to provide a server that returns Lotto numbers.

I don’t really want to reproduce it as a whole, but only the “greatest” lines of code.

It starts by neither using use warnings; nor use strict;. Please do that all the time.

For example:

foreach $nologing (@lottotip)
{
print "$nologing ";
}
print "\n";

Well, why easy if you can have it complicated? I guess most people would prefer:

print "@lottotip\n";

Another cool thing are the variable names, for example:

$sollichabbrechen="wahr";

This would translate to:

$shalliexit="true";

The main thing is the Lotto number generation function:

sub tip
{
   # Unterprogramm welches Lottotip erzeugt.
   # Gibt ein Array namens "lottotip" zurück.

“It returns an array called `lottotip’”. Wow. I didn’t know Perl had named arrays. ;-) :

   # Aufruf des Unterprogrammes erfolgt mit "&tip"

“You call this subroutine by `&tip’”. I guess the casual Perl programmer knows that. The “&” is optional in modern Perl, BTW. :

   srand; # Zufallsgenerator starten

You shouldn’t srand inside a subroutine, do it once in the main program. :

   @lottotip=(0,0,0,0,0,0);

   for($lottoschleife=0;$lottoschleife<6;$lottoschleife++)
    {
lottozufallwieder:
     # Zufallszahl erzeugen
     $lottozufall=int(rand(46));

     # Schauen ob zwischen 0 und 46
     if($lottozufall == 0 || $lottozufall > 45)
        {
        goto lottozufallwieder;
        }

$lottozufall=int(rand(45)+1); would have been easier. goto, yuck! (next exists, by the way.) :

     # Schauen das keine Lottozahlen im Tip doppelt vorkommen
     $lottoarraycheck=0;
     while($lottoarraycheck < 6)
        {
        if($lottozufall == $lottotip[$lottoarraycheck])
        {
        goto lottozufallwieder;
        }

You know goto is evil? :

        $lottoarraycheck++;
        }

     $lottotip[$lottoschleife]=$lottozufall;
    }
    return @lottotip;
   # Subfine
}

That was an awful way to write:

sub tip {
  return(sort { rand() <=> rand() } (1..45))[1..6];
}

Some code just shouldn’t be published…
and some people can write Basic in all languages. ;-)

NP: Pearl Jam—Porch

Copyright © 2004–2022