Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Strangely enough nobody's offered bad code they wrote

Is this a challange? Oh boy, do I have some bad code that I've written. Of course, I know better nowadays. But back then... boy oh boy. Let me dig through my dead code archive for some examples.

Let's start with a nice subroutine to convert a decimal number to its binary form:

sub bin { my $num = shift; my $bin = ""; for my $digit (128, 64, 32, 16, 8, 4, 2, 1) { if($num >= $digit) { $num -= $digit; $bin .= 1; } else { $bin .= 0; } } return $bin; }

The name is pretty bad to begin with, but the real problem is the brute-force algorithm. If only I'd known that I could've written it like this instead:

sub bin { substr unpack("B*", pack "i", $_[0]), 0, 8; }

Or even:

sub bin { sprintf "%08b", $_[0]; }

Then of course, there's code like this, which just makes me shudder:

if(-e "$bmfile") { &addlink("$bmfile","$url","$desc"); } else { &newbmfile("$bmfile","$url","$desc"); }

Or how about this doozy:

LINE: foreach $line (@bmfile) { chomp($line); if($line =~ /title/i) { ($null,$title,$null) = split(/\<\/*title\>/i,$line); } elsif($line =~ /a href/i) { ($null,$link,$null) = split(/\"/,$line,3); ($null,$desc) = split(/\>/,$line); ($desc,$null) = split(/\</,$desc); } else { next LINE; } if($link) { $links[$#links+1] = "$desc,$link"; } }

Wait, wait! There's more!

while(defined($line = <IN>)) { chomp($line); @chunks = split(/\s+/,$line); $ctr = 0; while(defined($chunk = $chunks[$ctr])) { if($chunk =~ /HREF/) { ($null,$url) = split(/\"/,$chunk); print "$url\n"; @full = split(/\//,$url); $dir = $full[5]; $file = $full[6]; system("wget $url"); unless(-e "$dir") { mkdir "$dir",0755; } rename "$file","$dir/$file"; } $ctr++; } }

I can keep going all day:

open(IN, "<$accesslog_file") or die "Cannot open $accesslog_file for input: $!\n"; while(defined(my $line = <IN>)) { chomp $line; if($line =~ /(.*?) - - \[(.*?)\/(.*?)\/(.*?):(.*?):(.*?):(.*?) ( +.*?)\] \"(.*?)\" (.*?) (.*?)/) { my $host = $1; my $day = $2; my $month = $3; my $year = $4; my $hour = $5; my $minute = $6; my $second = $7; my $timezone = $8; my $request = $9; my $response = $10; my $unknown = $11; $host = &dns_lookup($host); # push information into our hash # adds a reference to the log entry array into # the hash element for this host push @{ $hosts{$host} }, [ $month, $day, $year, $hour, $minute, "'$request'", $response ]; } } close(IN);

So yeah. Now that I've let some of this stuff out there, and everyone thinks I'm a terrible coder, are you happy? Did I live up to your challenge? :-)

Update: added <readmore> tags.

Update: I guess my willingness to post my own old, bad code gets me downvotes. How very pleasant.


In reply to Re^2: The joys of bad code by revdiablo
in thread The joys of bad code by BUU

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (6)
    As of 2014-04-17 03:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (439 votes), past polls