Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

TIMTOWTDI meets Rube Goldberg

by Tanktalus (Canon)
on Aug 06, 2011 at 04:24 UTC ( [id://918897]=perlmeditation: print w/replies, xml ) Need Help??

It's the weekend. Time to get your head out of the production-level code. But not too far - we'd go off-topic. So let's instead think about some of the worst ways to do things.

I don't mean things like `rm $file`;. I'm thinking more along the lines of a daily wtf, but for people who don't automatically think that "perl code" == "line noise". For example:

sub remove_newlines { my $string = shift; my $clean_string = `echo -n $string`; return $clean_string; }
This isn't merely chomp - this is super-chomp. Whether you have one or many new lines at the end of your string, this will clear 'em all out. If you're not shaking your head in disappointment over that code, you're probably not realising that s/\n+$// does the same job - but faster, more reliably (no shell metacharacters to worry about!) and is far more portable.

Sit back, crack open your favourite beverage, share code you'd be embarrassed to check in to source control or upload to CPAN, and enjoy your weekend. :-)

Replies are listed 'Best First'.
Re: TIMTOWTDI meets Rube Goldberg
by jdporter (Paladin) on Aug 09, 2011 at 18:14 UTC

    I once worked with a guy who was a tester but knew just enough sh and perl to be dangerous. He knew some, but not all, of the basic array operations. He didn't know push. To achieve the effect of push, he used the following code snippet:

    open F, ">tmpfile"; print F @array; print F "$one_more\n"; close F; open F, "<tmpfile"; @array = F; close F;

    (I think he lived with always having newlines on the end of his array elements.)

    To make matters worse, he did not know about subroutines; he copy-and-pasted the above snippet everywhere he needed to push onto an array.

    wtf!

    I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

      I've seen that before, but not in Perl. It's horrifying.

Re: TIMTOWTDI meets Rube Goldberg
by eyepopslikeamosquito (Archbishop) on Aug 07, 2011 at 01:36 UTC

    Though I've seen a lot of horrific Perl code, I can't remember any amusingly horrific Perl code right now. However, I was tickled the other day by this piece of Unix shell:

    #!/bin/sh # ... some_number=42 # ... if [ $some_number > 1 ] ...
    Presumably the intent was to check if $some_number is greater than one. What it actually did was create a file called "1" in the current working directory as a side effect of the test always passing! I was wondering where on earth the file called "1" in my current working directory was coming from. When I finally found the offending snippet above, I asked the perpetrator how he tested his script and was met with an embarrassed silence. He could see the funny side of it though.

Re: TIMTOWTDI meets Rube Goldberg
by graff (Chancellor) on Aug 07, 2011 at 04:49 UTC
    If you're not shaking your head in disappointment over that code, you're probably not realising that s/\n+$// does the same job...

    Um, I think you mean s/\s+$// does the same job. But anyway, one of the limitations of sharing really bad code is that it's typically too long and too boring to make for a good joke, usually because the most frequent symptom of bad code is copy/paste programming.

    I saw a script a few years back whose purpose was to produce a summary report every day, showing year-to-date quantities of data received from about two dozen sources -- so, roughly 24 lines of output. The solution? If "previous day" was the first day of January, no need to compute "year-to-date", so this makes up an "if" block with 24 distinct "print" statements, where the name of each data source is part of a distinct but otherwise identical string being printed, saying the count is 0 for each one. The else block was more "interesting": 24 copies of a 4-line block, where the source name was changed in 3 of the 4 lines. (And 2 of the lines were shell commands in back-ticks to assign strings to two variables being used in the print statements.)

    As a bonus, here was the method at the top of the script for getting yesterday's date:

    # get the date of yesterday my $nowsecond = time (); my $yestersecond = $nowsecond - 24 * 60 * 60; my @yesterdate = localtime ($yestersecond); my ($day, $month, $year) = ($yesterdate[3], $yesterdate[4], $yesterdat +e[5]); $day = sprintf("%.2d", $day); $month = sprintf("%.2d",$month+1); $year = $year + 1900;
    Based on this programmer's general concept of "logic", it's almost surprising that this part wasn't copied into each of the "if/else" blocks -- but it turns out he decided to get the previous day's date as well (though he never used that value), so he repeated the above block, adding a factor of 2 to the subtraction from "$nowsecond". And of course, both the "if" and "else" blocks contained their own copies of this essential "glue" operation:
    my $datestr = "$year.01.01-$year.$month.$day";
    This $datestr variable was dutifully copied into all 48 print statements.
Re: TIMTOWTDI meets Rube Goldberg
by iguanodon (Priest) on Aug 06, 2011 at 23:14 UTC
    You don't have to ask me twice to crack a favorite beverage... done.

    I've inherited a lot of crappy code, some of it in Perl (but to be fair, lots more in Java). A few years ago I had to change a script with this code to to add leading zeroes to day and month values less than 10:

    sub add_zero { $x = shift; return $x if ( $x > 9 ); return ( '0' . $x +) if ( $x < 10 ); }
    I'm not proud of this but I'll admit I "fixed" it with this:
    sub pad { my $n = shift; return $n if $n =~ /\D/; return $n > 9 ? $n : "0$n"; }
    I've never gotten around to fixing that because it always works... so far. But maybe this would be better? (suggestions welcome)
    $n = $n =~ /^\d/ ? sprintf('%02d', $n) : $n;
    There is lots more, mostly not mine, some definitely more WTF, but it will take some time to dig through the CVS history and pull out the most entertaining.

Re: TIMTOWTDI meets Rube Goldberg
by Logicus (Initiate) on Aug 06, 2011 at 17:51 UTC
    My entire source code library springs to mind...
Re: TIMTOWTDI meets Rube Goldberg
by sundialsvc4 (Abbot) on Aug 08, 2011 at 21:12 UTC

    After my third bottle of my favorite beverage, which by the way is delicious, “frankly, my dear, I don’t give a damn” about Perl.

    *Hic!*

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found