Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

TIMTOWTDI -- so long as it's not my way

by Ovid (Cardinal)
on Jan 16, 2001 at 01:53 UTC ( #52073=perlmeditation: print w/ replies, xml ) Need Help??

As we all know, TIMTOWTDI stands for "There is more than one way to do it." That's the spirit of Perl. Of course, sometimes we (I) make dumb mistakes. For your enjoyment, I present you with some of mine.

I was working on some code today and stared at the following snippet for ten minutes trying to figure out why it wasn't working:

if ( defined $end_tag && ! defined $end_tag ) { # do something }
Can you say "brain cramp" boys and girls? I knew you could.

There was another time when I inexplicably did a bitwise XOR on a code reference and couldn't figure out why the reference wasn't working.

One of my favorites (from this node) is some actual working code that was hacked by a friend who was new to Perl:

my @stuff = qw(one two three); my $length = (@stuff.length)/10; print $length;
Can you tell his background is Java? :)

Any other monks care to share some of their more humiliating mistakes?

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Comment on TIMTOWTDI -- so long as it's not my way
Select or Download Code
Re: TIMTOWTDI -- so long as it's not my way
by lemming (Priest) on Jan 16, 2001 at 01:58 UTC
    My most recent was print printf("%03d\n", $number); and it took way to long to figure out where the extra one was coming from.

    And another one from a long program that wrote a bunch of files, but got rid of any empty ones.
    # The code as it was executed open filehandles print "to file"; # This was anything from # a few to huge # of items #cleanup dir go through directory and unlink any empty files close filehandles
    For some reason if the program didn't have much to say I'd wind up with fewer files than expected.
      Hmm I just did sprintf "%02d%02d", @nums, $/; today. After a while of trying to figure out why it didn't print anything I said, Ahh! and took the "s" off the front.

      THEN began the puzzling over why the newline wasn't printing. =)

      --
      $you = new YOU;
      honk() if $you->love(perl)

Re: TIMTOWTDI -- so long as it's not my way
by jynx (Priest) on Jan 16, 2001 at 02:11 UTC
    i think my favorite mistake so far is:
    open FILE, $file or die "Couldn't open file: $!"; my @file = <FILE>; foreach (@file) { s/ome/substitution/ if ($applicable); } close FILE;
    i couldn't understand for two days why the output was always a blank file. (i embarassingly had to hand it to a friend for him to notice i wasn't writing the file out! : )

    Anyway, ++ Ovid, i fell out of my chair laughing from that code. Thanks for the good cheer.

    jynx

      Just telling someone else about a problem is often all you need to realize what the problem (and the solution) is.

      /J

        A friend of mine called that "The Mannequin Effect" and just wanted a mannequin to wheel around the office to people who knew they were doing something stupid, but couldn't figure out why. That way you wouldn't pull someone off work just to have you go "Doh!"
Re: TIMTOWTDI -- so long as it's not my way
by boo_radley (Parson) on Jan 16, 2001 at 02:36 UTC
    Several dozen instances of

     if ($foo =>$bar) {...} meaning, of course

     if ($foo >=$bar) {...}

    *sigh*

      Oops. The way to remember that symbol is to remember that it is called "Greater than or equal to". If you can remember that, you'll always write >= and never =>, and then similarly <= is "Less than or equal to."

      Of course, if you're one of those freaks who goes around saying "equal to or less than", then you're doomed.

Re: TIMTOWTDI -- so long as it's not my way
by danger (Priest) on Jan 16, 2001 at 02:37 UTC

    I've done this one twice:

    print join '|', @array, "\n";

    and both times tried to figure out how that empty string got on the end of my array before coming to my senses.

    Update: Oh, and I might as well admit, both times the statement was a toss-in just to make sure everything was as expected so far -- go figure :-)

Re: TIMTOWTDI -- so long as it's not my way
by kschwab (Priest) on Jan 16, 2001 at 03:02 UTC
    Mmm...I have a perlmonks "doh".

    I missed a sleep() here, which would start an awful busy loop on anyone's server who took the advice.

    To further compound the problem, It looks like I unintentionaly started a flame war with Dominus with a poorly written interpretation of my thoughts.

    Hopefully I'll make better use of the preview button now :)

      Syas kschwab:
      It looks like I unintentionaly started a flame war with Dominus with a poorly written interpretation of my thoughts.

      If I did anything to give the impression that I was flaming you, I apologize. I didn't mean to flame.

      Besides, as you pointed out, I was mistaken. I said your loop would always run a million times, and I was wrong, so it's no wonder that you tried to find some interpretation of my message that would have made more sense.

      Hopefully I'll make better use of the preview button now :)

      Me too.

        Heh....

        No hard feelings here...you are correct, it wasn't really a flame war.

        After looking at your home node, it would appear that I'd be a bit out of my league if it really was a war :)

Re: TIMTOWTDI -- so long as it's not my way
by wardk (Deacon) on Jan 16, 2001 at 07:44 UTC

    I seem to do this more often than I should

    my $this = $q->param("this"); if ( $this = "that" ) { # do something for possibly the wrong reason } else { # not likely }
Re: TIMTOWTDI -- so long as it's not my way
by jepri (Parson) on Jan 16, 2001 at 09:48 UTC
    Yup. Even worse, I posted this in the chatbox and asked why it wasn't working:
    my $thing; while $something (@somethingelse) { my $thing; ....; print $thing; }
    At first I couldn't understand why it was overwriting $thing, then I asked the CB why I wasn't getting a warning for overriding $thing. That was a bad code day. :)

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: TIMTOWTDI -- so long as it's not my way
by cat2014 (Monk) on Jan 16, 2001 at 10:04 UTC
    one of the worst things that's ever happened to me was learning pascal as my first serious language. to this day, 90% of the time when i want to get the first element from an array, i automatically type $array[1]. then i spend 3 minutes looking at the funky output, remember that i always screw up arrays, and go fix it. you'd think that after about 2 years of doing this i'd stop.

    is there even any other language which uses array[1] instead of array[0] to access the first element?

      Some dialects of BASIC use one-based arrays, IIRC... I think FPBASIC is one such BASIC, tho I'm too lazy to fire my Apple up to test it right now. :o)

      --
      Me spell chucker work grate. Need grandma chicken.

      Says cat2014:
      is there even any other language which uses array[1] instead of array[0] to access the first element?

      Fortran.

      Actually most versions of BASIC brought out by MS used array(1) as the base. But in an unparalleled moment of clarity they included a command that allow you to set the first index of the array to 1 OR 0. I think it was called OPTION BASE

      ____________________
      Jeremy
      I didn't believe in evil until I dated it.

      PostgreSQL is a more contemporary example of something that uses 1-based arrays. Although it's contrary to the concept of relational databases, PostgreSQL lets one define columns that can contain arrays of basic types (like an array of int). These arrays are 1-based.

      (Since your question leads us off the Perl path anyway...) Smalltalk does, but with different syntax. For example, take the following Array of Strings:

      | temp | temp := #('one' 'two' 'three'). Transcript show: (temp at: 1).

      The line Transcript show: (temp at: 1). will print the string "one".

      My training was with C++/C, and on the new job, we get to use Smalltalk. So I've been having the opposite problem that you've been having. :-)

      While we're completely off-topic (and no one's around anymore to --me for bringing up this one ; ),

      If i remember correctly Ada allows any base for the first element of an array. So you could have an array from 64 to 128 if you wanted.

      That would be really confusing if you had different array bounds for every array!

      jynx

Re: TIMTOWTDI -- so long as it's not my way
by Dominus (Parson) on Jan 16, 2001 at 11:44 UTC
    Says ovid:
    if ( defined $end_tag && ! defined $end_tag ) { # do something }

    That reminds me of the people who do

    if ($num != 3 || $num != 4) { # do something }

    ...and then wonder why it doesn't do what they wanted. I'm not sure if it's more embarrassing or less so.

(kudra: Die, Die! DIE!) Re: TIMTOWTDI -- so long as it's not my way
by kudra (Vicar) on Jan 16, 2001 at 14:25 UTC
    I know that I've done plenty of those doh things myself, but I seem to have erased them from my memory, so I'll have to talk about other people's code I've run into at work ;)

    Recently I ran across the efforts of an apparently homicidal predecessor when I was asked to fix a project that contained a line that looked a bit like this:

    $dbh = DBI->connect("DBI:$dbm:$db:$server", $user, $pwd) && die ("Unable to connect to database $db: $DBI::errstr\n");

    I might have ruled that an accidental death, but it was really the start of a rampage. In the project which consisted of perhaps 10 files, this was done every single time that 'die' was used.

Re: TIMTOWTDI -- so long as it's not my way
by turnstep (Parson) on Jan 16, 2001 at 18:52 UTC
    I recently could not sleep and decided to do some coding (anyone else do that?) and deep in the bowels of my program came up with this gem:
    seek(FOOBAR, 2, 0);
    This appeared right before I was to write information (e.g. append) to the end of a file. Needless to say, the error was discovered very quickly! Luckily, I had a backup of the file in question. :)
Re: TIMTOWTDI -- so long as it's not my way
by Elgon (Curate) on Jan 16, 2001 at 23:07 UTC
    Oooh, getting lots of replies on this one: I think the problem is when you focus too much on the form of the code and not what you are actually trying to achieve. I have written some (rarely, I admit) lovely code but when I try it I am horrified when it doesn't work.

    Later, usually after a nicoteine and Dr.Pepper fix, when I look at it again I suddenly notice the truth:

    There is no problem with the code. Instead, try to realise that the problem is not in the code but... Thinks: I've still not got the Matrix out of my system... comes from your assumptions that you have built into the code.

    I do like the

    if ($foo = $bar && $foo != $bar) { #code }
    though, it's a nice cockup.

    Elgon

(jeffa) Re: TIMTOWTDI -- so long as it's not my way
by jeffa (Chancellor) on Jan 17, 2001 at 00:04 UTC
    A bit late on posting, but since everyone else is being so honest . . .

    Recently I was working on a little code - I took some lines and made them into a subroutine. Then I did a test run, and for 10 minutes couldn't figure out that I forgot to actually call my new subroutine.

    And then, just today something like this:

    # do section 1 stuff exit(); # do section 2 stuff
    Hmmm, why isn't section 2 executing?
    (of course, that exit is harder to see burried in actual code)

    Jeff

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
Re: TIMTOWTDI -- so long as it's not my way
by TGI (Vicar) on Jan 17, 2001 at 00:11 UTC

    The error I make, over and over, is putting a semicolon where it doesn't belong.

    sub foo ($$); { while ($bar); { $bar = 0; print << "HERE_DOC"; This doesn't print for some reason HERE_DOC } }

    I'm finally starting to recognize the various errors this generates. At this rate I'll only need to repeat this error about a thousand more times.


    TGI says moo
Re: TIMTOWTDI -- so long as it's not my way
by EvanK (Chaplain) on Jan 17, 2001 at 00:19 UTC
    For 3 days I couldnt figure this one out...
    open(FILE,"..."); format = blah . write; close(FILE);

    ______________________________________________
    When I get a little money, I buy books. If I have any left over, I buy food and clothes. -Erasmus

      I just remembered another one...I was writing a trivia grading script, and in the data file where I kept the questions and correct answers, without realizing what I was doing, I used \0 as the delimiter. then I couldnt figure out why the data file wouldnt write like it was supposed to. (if that makes sense to anyone other than myself)

      ______________________________________________
      When I get a little money, I buy books. If I have any left over, I buy food and clothes.
      -Erasmus

Re: TIMTOWTDI -- so long as it's not my way
by leons (Pilgrim) on Jan 18, 2001 at 14:30 UTC
    Okay .... I have just been staring at the following for over an hour ...
    Being totally amazed by the fact that it returned an Odd number of elements
    in hash assignment
    Okay ... stuff like that happens ... stuff like that happens
    a lot ;-) but the frustrating part is that, I made the same mistake a week ago as
    well and then it took me the whole morning .. hmmm ... I think I'm going to have
    another cup of coffee ;-)

    sub MySub { my (%Information,@Errors); # blah blah push @Errors,"an Error occured\n" if $whatever; return(\%Information,\@Errors); } my ($InformationRef,$ErrorsRef)=MySub; my (%Information,@Errors)=(%{$InformationRef},@{$ErrorsRef}); # !!!
Re: TIMTOWTDI -- so long as it's not my way
by radagast (Sexton) on Jan 19, 2001 at 16:40 UTC
    Here are a couple of enormously annoying mistakes. First, as my background before Perl (B.P.?) was C/C++, I always had fun forgetting to prefix my variables with $ or better yet, getting careless with Perl's flexibility and not initializing some variables before they were used.

    Second, this is more to do with the keyboard I use but it affects most of my work which is in Perl. I have a British keyboard and the British Pound happens to be next to the $ and occasionally my finger slips. Through telnet, I was always accidently placing pounds and whenever the scripts executed, I always got something along the lines of '\024 not found'. The pound never showed up. Only by using a windows program, UltraEdit, did I catch the mistake. Hugely embarrasing.
    ---
    "I slept with Faith, and found a corpse in my arms on
    awaking; I drank and danced all night with Doubt, and found
    her a virgin in the morning." Aleister Crowley, _The Book of Lies_

q(),qq(),qw(),qx()...AARGH!
by Madams (Monk) on Mar 17, 2001 at 06:05 UTC
    #/usr/bin/perl -w use strict; #much other stuff.... #initializations etc.... foreach $file (@list_of_filenames){ open(THISFILE,'< $file') or warn "ERROR: Can't open $file!\n";#for + this application fail to open is not fatal.. binmode(THISFILE); $data = Digest::MD5->new->addfile(*THISFILE)->hexdigest; return unless ($data); #...process $data..... }
    Gee, why does every file have the same digest?....
    Maybe because open tries to open a file NAMED: " < $file " ?
    I keep tripping on the q() vs qq() vs qw() vs qx()
    differences....

    _________________
    madams@scc.net
    (__) (\/) /-------\/ / | 666 || * ||----||

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-07-30 23:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (241 votes), past polls