I just spent three frustrating, heartbreaking, suicidal hours catching this bug. I was getting to the point that I started doubting my understanding of the most basic perl.
my @run = MyClass->find_all(); if(@rum){ # do something, }else{ #do something else }
The problem was that the "if" test always fail where it shouldn't. And I caught the bug after I put "print Dumper(@run)" after the first line, in other words, I was willing to accept that may be "if(@rum)" isn't what I think it is after all!

Of course, the bug was that I wrote "if(@rum)" instead of "if(@run)" (and my emacs font/coloring made it barely distinguishable). Why didn't I use strict? because I always use strict, I tell everyone to use strict, I've used strict all my life, and somehow I didn't this time, and I thought I was using strict!

I read some recent post here to the effect that "you don't have to use strict if you know what you're doing", I couldn't disagree more. I'd say, use strict, check twice you're using strict, and ask someone else to check you're using strict. You will live longer if you do.

Replies are listed 'Best First'.
Re: That damn "strict"
by revdiablo (Prior) on Sep 15, 2005 at 23:14 UTC
    I read some recent post here to the effect that "you don't have to use strict if you know what you're doing", I couldn't disagree more.

    Not to necessarily contradict your advice, but often times when I say something like, "you don't have to use strict all the time," I mean that sometimes it's honestly, for reals, truly ok to turn it off here and there. Using the no pragma. In a tight scope. Sparingly. :-)

      To further elaborate, I would put forward that one should turn off the specific aspect of strict you wish to violate within that specific block. Don't no strict; when you really mean no strict 'refs';.

      This is very similar to when I use which quoting mechanism. I use single-quotes nearly exclusively, except when I either have to use single-quotes within that string or if I'm interpolating a variable. And, double-quotes, to me, specifically indicate that one of those two situations is occurring. (This has caused me no end of grief in Javascript because IE won't auto-create a String object from a string created with single-quotes, only one created with double-quotes.)


      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
        I have the opposite quoting problem.

        I am in the habit of always quoting with ", so that I won't have to change it if I later decide to interpolate a variable.

        Which gives me no end of grief when it comes to SQL, because I constantly use " for strings and then wonder why it is complaining about my identifiers.

Re: That damn "strict"
by Hue-Bond (Priest) on Sep 16, 2005 at 08:39 UTC

    I usually start programs in the command line, typing just 'perl' and coding until I press ^D. I got sick of doing use warnings; use strict; all the time so I just did an alias perl='/usr/bin/perl -Mstrict -w'. Whenever I don't want that, it suffices to run /usr/bin/perl.

    $ perl print $c; Global symbol "$c" requires explicit package name at - line 1. Execution of - aborted due to compilation errors. $ /usr/bin/perl print $c; $ _

    --
    David Serrano

      Once you type "perl" this way and add some commands, how do you tell it to actually run the commands?

        Simple: by hitting Ctrl-D :^).

        --
        David Serrano

Re: That damn "strict"
by ruoso (Curate) on Sep 16, 2005 at 20:04 UTC

    Maybe a change to CPerl Emacs mode to alert you that there isn't a "use strict;" token in your file would help... It would be really nice... but I really don't understand a single line of Lisp...

    daniel
Re: That damn "strict"
by sfink (Deacon) on Sep 19, 2005 at 17:21 UTC
    I don't understand. Your code is fine, as long as you don't forget the *rum = *run line before the start of it. I don't get what this 'strict' thing is that you're talking about; that'll just make it complain about global symbols or something.

    This does point out, however, that the case-insensitivity of filesystems that everybody agrees is the right thing now that Microsoft has proven it correct, doesn't go far enough. If Perl really DWIM, then it wouldn't require the *rum = *run line; it would know that 'n' and 'm' really shouldn't be distinguished anyway. These are variables, people, not taxonomic identifiers or proper nouns -- why do we need base 63 ([a-zA-Z0-9_]) to distinguish them when the typical program has barely a dozen active at the same time?

      This is all fixed in Perl 6. We even got rid of "use strict". Plus if you program in the Haskell subset, you don't need variables at all, so you can't misspell them...
Re: That damn "strict"
by Anonymous Monk on Sep 20, 2005 at 14:15 UTC
    So you forgot both strict and warnings? Or did you use @rum somewhere else as well?

    When I start a program, I hit a keystroke and my editor puts in the appropriate she-bang line (or package statement - cleverly looking at the filename), adds 'use strict' and 'use warnings', and turns 'perl-mode' on.

    Good editor. Nice editor. Perl powered too.