Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by oko1 (Deacon)
on Feb 26, 2012 at 04:30 UTC ( [id://956183]=note: print w/replies, xml ) Need Help??


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

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"

Replies are listed 'Best First'.
Re^3: Help! My variables are jumping off a cliff!
by Jenda (Abbot) on Feb 26, 2012 at 14:26 UTC

    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"
Re^3: Help! My variables are jumping off a cliff!
by tangent (Parson) on Feb 26, 2012 at 04:48 UTC
    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"
        I hear what you're saying oko1, but isn't use warnings a valid response? I mean, where would you draw the line with strict if you start looking at all the other problems you can run into?
Re^3: Help! My variables are jumping off a cliff!
by muba (Priest) on Jan 29, 2013 at 04:39 UTC

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://956183]
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: (4)
As of 2024-04-25 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found