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.
| [reply] [d/l] |
|
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"
| [reply] |
|
... 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.
| [reply] [d/l] |
|
|
|
Re^3: Help! My variables are jumping off a cliff!
by BrowserUk (Patriarch) 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:
- The strict module has had a bug all these years and nobody noticed until you did.
- 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".
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re^3: Help! My variables are jumping off a cliff!
by tobyink (Canon) 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. | [reply] [d/l] [select] |
|
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"
| [reply] |
|
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';. | [reply] [d/l] [select] |
|
|
|
|
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.
| [reply] |
Re^3: Help! My variables are jumping off a cliff!
by ikegami (Patriarch) 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;
| [reply] [d/l] |