Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Help! My variables are jumping off a cliff!

by tangent (Deacon)
on Feb 26, 2012 at 04:05 UTC ( #956178=note: print w/ replies, xml ) Need Help??


in reply to Help! My variables are jumping off a cliff!

strict vars will only prevent declarative suicide, not kamikaze coding :)


Comment on Re: Help! My variables are jumping off a cliff!
Re^2: Help! My variables are jumping off a cliff!
by oko1 (Deacon) on Feb 26, 2012 at 04:30 UTC

    Could you define "declarative suicide", please?

    I'm also not very clear on why you'd consider the problem that I'm demonstrating as an example of "kamikaze coding". Imagine a script in which you've already declared a lexically-scoped $foobar; a couple of hundred lines later, you decide that you need a new variable name, and $foobar seems like a reasonable thing. Create it, and - presto - you've destroyed the previous one. Is that "kamikaze coding"? Or is it a language problem?

    #include <stdio.h> int main(void) {int x = 1; int x = 10; printf("%d\n", x);}
    ben@feynman:/tmp$ cc x.c x.c: In function ‘main’: x.c:3:33: error: redefinition of ‘x’ x.c:3:22: note: previous definition of ‘x’ was here
    -- 
    I hate storms, but calms undermine my spirits.
     -- Bernard Moitessier, "The Long Way"
      I would consider your example a coding problem, a mistake I would expect warnings to catch, not strict. My original comment was just a play on words - my( declare ), kamikaze( specific type of suicide )

        OK, thanks for clarifying. I'm just seriously surprised that Perl, which is an extraordinarily sensible language in most other ways, doesn't have a more definite response to this kind of mistake - one that can easily be made unintentionally - in coding. I'm even more surprised that it doesn't seem to bother other people.

        -- 
        I hate storms, but calms undermine my spirits.
         -- Bernard Moitessier, "The Long Way"

      If your variables have several hundred lines wide scopes, you've got a problem. Such a variable is just very very very slightly better than an outright global.

      Keep in mind that Perl has BLOCK scoping, not subroutine scoping. So a variable declared within a block (whether it's a block that belongs to a loop, an if() or just a bare block) only "exists" within that block, not in the whole subroutine. So do declare your variables in the smallest possible block and you will not have to worry about accidentally "destroying" a variable. You can (and at times should) even create blocks just to restrict the scope of a variable!

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

        If your variables have several hundred lines wide scopes, you've got a problem. Such a variable is just very very very slightly better than an outright global.

        Agreed, totally - and it's not an error that an experienced programmer is going to commit. But that can be said of many, if not most, errors that "warnings" catches - and we typically tell people to "always - yes, ALWAYS - enable warnings." I'm saying that this is a dangerous error, and one that should be noted and treated as such rather than some minor, optional after-thought.

        -- 
        I hate storms, but calms undermine my spirits.
         -- Bernard Moitessier, "The Long Way"

      Then again, one could argue some refactoring is long overdue when you have a scope that lasts for, I quote, a couple of hundred lines.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-09-03 07:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls