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

sometimes use strict isn't enough

by coolmichael (Deacon)
on Oct 24, 2001 at 09:17 UTC ( #121001=perlmeditation: print w/replies, xml ) Need Help??

So, here I am, happily writing a nice simple little CGI to retrive some a row from a database. It's easy. I've tested the SQL statements. I've tested the perl. Everything is fine, so long as I don't include the parameter. It's a simple parameter too. $id is the parameter, it's nice and easy to remember. $pid is the name of the parameter on the form in the html. So, guess what I've been using? $pid when I meant to be using $id.

Moral of the story: Don't rely on strict. It's only there to help you doing dumb things. If you insist on stupidity, it can't help.

Edit kudra, 2001-10-24 Changed title; removed broken HTML

Replies are listed 'Best First'.
Re: sometimes use strict isn't enough
by dragonchild (Archbishop) on Oct 24, 2001 at 16:58 UTC
    What this says is that strict is good ... good programming techniques are better.
    1. Use meaningful (read: unmistakeable) variable names
    2. Declare things in the smallest scope they can be used in.
    I suspect that had you used #2, strict might've caught it for you.

    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: sometimes use strict isn't enough
by rob_au (Abbot) on Oct 24, 2001 at 12:44 UTC
    *chuckle* ... This is one which -w would probably have picked up for you depending on the scoping of the variables involved.

    I meditated on a similar point recently here following a review of some code I was looking at for another project.


    Ooohhh, Rob no beer function well without!

(jeffa) Re: sometimes use strict isn't enough
by jeffa (Bishop) on Oct 25, 2001 at 02:01 UTC
    What you are refering to is the difference between compile-time errors and run-time errors. use strict does not catch run-time errors. Matter of, fact, the only thing that can catch run-time errors is a pair of eyes - someone else's pair of eyes usually sees it before your pair of eyes!! :D (figuratively speaking)

    Good example, Perl will happily compile and run this without complaining in the least - bonus points for an explanation why:

    foreach (@list) { $conn-do("insert into foo values($_)") || die; }
    when i really meant this:
    foreach (@list) { $conn->do("insert into foo values($_)") || die; }
    So, DO rely on strict to check for the things that strict can verify. It's your brain that you shouldn't rely on - mine get's me in trouble every time at least ;)


      One slight correction...

      strict 'subs' and strict 'vars' run their checks at compile time, but the third goblin in that group strict 'refs' enables runtime checks.

      (sorry, I was inspired by the dancing skeleton on your homenode ...... ;-) )

      Consider the following code, which will toss a strict error during runtime, depending on the flip of a coin:

      #!/usr/bin/perl -wT use strict; use vars qw($foo $bar); $foo = 'x'; $bar = int(rand(2)) ? 'foo' : \$foo; print $$bar, "\n"; =OUTPUT (when rand==0) x =OUTPUT (when rand==1) Can't use string ("foo") as a SCALAR ref while "strict refs" in use at ./ line 9.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://121001]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2017-11-24 11:34 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (348 votes). Check out past polls.