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

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.

Replies are listed 'Best First'.
Re^4: Help! My variables are jumping off a cliff!
by oko1 (Deacon) on Feb 26, 2012 at 15:02 UTC

    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"
      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';.

        I have indeed shown that it is an error - in the post preceding the one where I made that statement. To recap: you declare a variable in one part of a large program. Later, when adding code, you unintentionally declare the same variable again in another part of the program. You've just smashed the first one, and caused errors in any code that depended on it.

        The error is not that $x now contains 10. The error is that this can happen at all, with minimal (if any, depending on externals) warnings.

        -- 
        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.