Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Plain old bad code

by HuckinFappy (Pilgrim)
on May 20, 2006 at 04:29 UTC ( [id://550635]=perlmeditation: print w/replies, xml ) Need Help??

This probably doesn't belong anywhere, but Meditations is as good a place as any I suppose I was crawling through someone else's code today. I was supposed to go port it to use some new infrastructure of ours, but after I clawed my eyes out, we decided I should rewrite it. The clincher for me was:
for ( @data ) { sysopen ($fh, "$log", O_WRONLY|O_APPEND); $fh->print ("$_"); close $fh; }
This sent me off on a rant. This sent my manager and I off into a tailspin inventing god-awful code. My final contribution is nothing short of contrived ugliness, and I wanted to share it with you, my esteemed fellow monks:
#!/usr/bin/perl my $filename = $ARGV[0]; my @size = grep /./, map { $_ } split //, `wc -l $filename`; my $i = ''; my $loop = 1; while ( $loop ) { my $d = shift @size; if ( $d =~ /\d/ ) { $i .= $d; } else { $loop = 0; } } for ( 1 .. $i ) { my $line = `head -$_ $filename | tail -1`; print $line; }
Since I'm dealing with the ugliness someone else unleashed on the world, somehow I feel better having unleashed that on the world, and it's better to do it here than in real code. This isn't about obfuscation, it's about just plain god awful code that does the job (in this case a version of 'cat'). A reminder of what we strive to not unleash on people. So make me cringe...show me your ugliness.

Replies are listed 'Best First'.
Re: Plain old bad code
by grinder (Bishop) on May 20, 2006 at 10:10 UTC

    What's with the

    for ( 1 .. $i ) { my $line = `head -$_ $filename | tail -1`; print $line; }

    As any fule no, that should be written as

    my $line; my $ln = 0; for ($ln = 1; $ln <= $i; $ln = $ln + 1) { system ("head -$_ $filename | tail -1 > file.tmp"); open(FILE, "file.tmp"); my $tmp = $_; while (<FILE>) { chomp; print $_; print "\n"; } close FILE; $_ = $tmp; }

    I'm very proud of the masterful technique used to avoid clobbering $_

    • another intruder with the mooring in the heart of the Perl

      Could you explain to a newbie what do you mean by clobbering $_ and why that is bad?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://550635]
Approved by willyyam
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-03-28 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found