### 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") {
} else {
&newbmfile("\$bmfile","\$url","\$desc");
}

```  LINE: foreach \$line (@bmfile) {
chomp(\$line);
if(\$line =~ /title/i) {
(\$null,\$title,\$null) = split(/\<\/*title\>/i,\$line);
} elsif(\$line =~ /a href/i) {
(\$null,\$desc) = split(/\>/,\$line);
(\$desc,\$null) = split(/\</,\$desc);
} else { next LINE; }
}

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: I guess my willingness to post my own old, bad code gets me downvotes. How very pleasant.

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!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• 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
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2016-05-30 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What font do you use for programming?

Results (373 votes). Check out past polls.