Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by oko1 (Deacon)
on Feb 26, 2012 at 04:45 UTC ( #956184=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!

Declaring a variable twice isn't an error, just unnecessary

Declaring a variable twice in the same scope is indeed an error - and of exactly the same type as 'strict' is supposed to prevent. Perl may not throw a warning about it as a default behavior, but that does not make it not an error. Is there ever a situation in which declaring a variable twice in the same scope is useful? Conversely, is there ever a time when doing so will not cause problems?

It seems to me that reasonable answers to both of the above questions imply that this should indeed be a feature in "strict" - if not in Perl itself. It would have rather obvious positive effects, and no negative ones that I can think of.

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


Comment on Re^2: Help! My variables are jumping off a cliff!
Re^3: Help! My variables are jumping off a cliff!
by BrowserUk (Pope) on Feb 26, 2012 at 05:34 UTC
    Declaring a variable twice in the same scope is indeed an error ...

    By who's definition? Certainly not Perl's. Or strict's.

    and of exactly the same type as 'strict' is supposed to prevent.

    And yet -- it doesn't. Which could mean one of two things:

    1. The strict module has had a bug all these years and nobody noticed until you did.
    2. You've misunderstood.

    I'll let you decide which you want to believe.

    And while you do that, I'll continue to believe the evidence of the implementation, and bet you that nothing you say or do will make it change in the future.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      By who's definition? Certainly not Perl's. Or strict's.

      BrowserUk... frankly, I'd rather you didn't bother responding to any more of my posts. I've seen quite a few of your responses to others, and have experienced a number of them myself, and you are a small-minded, sniping, intentionally-obtuse troll. You leave a nasty, clinging miasma on everything you touch, and PerlMonks is significantly poorer for your presence, despite your competence at Perl; I have little doubt that you have been the cause of a number of people leaving here, and I have felt like doing so myself on occasion.

      Feel free to keep scoring my posts down, you vicious little weasel, but do go away. I won't be responding to anything you write any longer.

      -- 
      I hate storms, but calms undermine my spirits.
       -- Bernard Moitessier, "The Long Way"
        I'd rather you didn't bother responding to any more of my posts.

        That's cool. I'll try to make a mental note of your handle. (BTW: To my knowledge, I have never down-voted a single one of your posts. Maybe a God could check that for me?)

        When logic fails -- distract.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

        PerlMonks is significantly poorer for your presence
        I really, strongly disagree. He took the time to clarify that a statement you made was incorrect. Granted it was done without politeness...but what are you going to do? You came with a question and he answered it. And he did so without calling you names, so I'd note his politeness is exceedingly beyond yours.
Re^3: Help! My variables are jumping off a cliff!
by tobyink (Abbot) on Feb 26, 2012 at 07:21 UTC

    Is there ever a situation in which declaring a variable twice in the same scope is useful? Conversely, is there ever a time when doing so will not cause problems?

    Yes - when a variable is no longer needed, then there's no harm in redeclaring it and using it for a different purpose.

    For example, imagine the following test script:

    use Test::More; use strict; no warnings; diag "Testing that 'uc' works."; plan tests => 3; my $result = uc 'foo'; is $result, 'FOO'; my $result = uc 'bar'; is $result, 'BAR'; my $result = uc 'baz'; is $result, 'BAZ';

    Certainly different variables could be used for each test, or the my could be dropped from the second and third tests, but why should Perl insist upon it. The code above is perfectly clear, and runs fine.

      Yes, it's possible to do. I already knew that (and had, in fact, demonstrated it in my very first post.) That's not what I was asking about.

      Because, you see, I have also shown that it can be, and usually is, an error - one so bad that it already has a name in common usage (variable suicide.) My contention is that errors of that sort need to be highlighted very strongly - and most other languages do so, generally by failing at compile time. Perl itself does nothing about it. In fact, even the additional warnings mechanism considers it a 'misc'-category warning; for example, using common::sense, which selects a "sensible set of warnings", eliminates that warning entirely.

      You have re-demonstrated that it can be done. That's not the same thing as showing that it is useful - and even if it was... smoke alarms are installed in bedrooms and plane lavatories for a very good reason, one that the majority of us humans agree with: that it can do much more damage than any good that it prevents.

      My contention is that this type of error should be taken much more seriously than it is, for that same reason.

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

        It was certainly not my intention to merely demonstrate that it can be done. That has been established beyond any doubt. You had asked:

        is there ever a time when doing so will not cause problems?

        My example is a case where redeclaring the variable does not cause problems.

        I have also shown that it can be, and usually is, an error
        You haven't. Your OP shows some code, but it's not clear to me whether there's an error. But even I grant you that you have shown that it's possible to make an error, you have utterly failed to even remotely convince me of the "usual" part.

        To go back to your original code:

        my $x = 1; # Variable declaration my $x = 10; # Variable suicide! print $x;
        What's the error here? This prints 10. Should this have printed 1? Do note that if you'd had written:
        my $x = 1; $x = 10; print $x;
        it would have still printed 10.

        You know, it happens probably a few times a week that my code triggers the "masks declaration" warning. I cannot recall a single time where this was actually causing a problem. Just removing the "my" fixes the problem; or a no warnings 'misc';.

Re^3: Help! My variables are jumping off a cliff!
by chromatic (Archbishop) on Feb 26, 2012 at 07:41 UTC
    Declaring a variable twice in the same scope is indeed an error...

    my does two things. During compilation, it creates an association between a variable name and its appropriate lexical scope. During runtime, it initializes the appropriate storage for that variable.

    The strict pragma only cares about the former; the documentation has long stated:

    This generates a compile-time error if you access a variable that wasn't declared via "our" or "use vars", localized via "my()", or wasn't fully qualified.

    While you may be right that strict should warn about double declaration, strict 'vars' only concerns itself with an idempotent operation. You can't create that compile-time association more than once in the same scope because that association is an either-or concern.

    With that said, you can't unilaterally assume that all instances of the double-declaration have unintended runtime consequences. Those consequences are also well established and long understood. I can imagine code which does this deliberately.

    That's not my particular style, but not everything I find confusing is (nor should it be) an error.


    Improve your skills with Modern Perl: the free book.

      my does two things. During compilation, it creates an association between a variable name and its appropriate lexical scope. [...] The strict pragma only cares about the former

      Agreed (and thank you! - having my point addressed directly is like a breath of fresh air.) So, given that 'strict' "cares" about the association between a variable name and its lexical scope... doesn't it make sense that it should check that scope for that name already existing in it?

      With that said, you can't unilaterally assume that all instances of the double-declaration have unintended runtime consequences.

      But I don't assume that. I'm saying that this is usually a dangerous error that could be caught by 'strict' - and if, for whatever reason, you actually need this to happen in your code, we have the 'no' pragma. This is a well-established practice in Perl for a variety of errors of this sort, and it just seems to me that this type of error would fit very well into that process, without creating any problems. I'd be willing to bet significant amounts of money - say, a nickel :) - that doing so would A) not harm anyone's correctly-written code and B) perhaps discover buried errors.

      It seems to me that this would be an obviously Good Thing with no negative consequences. Hence my surprise at its absence.

      -- 
      I hate storms, but calms undermine my spirits.
       -- Bernard Moitessier, "The Long Way"
        ... doesn't it make sense that it should check that scope for that name already existing in it?

        I don't see how that follows. To me, that's like saying that exists on a hash key should return a different answer depending on how many different values that hash has had associated with the key.

        You can convince me that having to enable both strict and warnings is silly busywork (because it is), but adding features from warnings to strict makes little sense to me.

Re^3: Help! My variables are jumping off a cliff!
by ikegami (Pope) on Feb 27, 2012 at 21:24 UTC

    Declaring a variable twice in the same scope is indeed an error -

    No, it's not. The following code works just fine:

    my $x = 1; say $x; my $x = 2; say $x;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-12-29 11:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (187 votes), past polls