New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lexical scoping of "my" quite odd in postfix loops #7140
Comments
From @jlokierCreated by @jlokierGuess what this program prints? use strict; Good guess! $a = :: The values for $a and $b are very surprising. I'd expect them to It's not that surprising to get undef. This must necessarily assign my $n = 1 if some_random_condition(); What's surprising is to get undef when the loops do execute at least once. This code maybe illustrates what is so unexpected: use strict; The sensible (and DWIM) behaviours I *strongly* expect are $x == 9 That's too surprising. I suggest changing the semantic to one of the Thanks, Perl Info
|
From @rgsJamie Lokier wrote in perl.perl5.porters :
Using the terms "outer" and "inner" is misleading. The perlsyn manpage documents this bug, saying that "the behaviour of a
The plan is to add a deprecation warning for the cases where is |
The RT System itself - Status changed from 'new' to 'open' |
From @iabynOn Tue, Mar 09, 2004 at 08:50:20AM -0000, Rafael Garcia-Suarez wrote:
Yes, if run with 'use warnings', you get: "my" variable $a masks earlier declaration in same scope at /tmp/p line 7. -- |
From @jlokierDave Mitchell via RT wrote:
Ok, but that's not really the point of my bug report. Sorry if that Please consider this program: use strict; The point is that $a is still visible after the loop, and is -- Jamie |
From @jlokierRafael Garcia-Suarez via RT wrote:
Yes, I was meaning in the sense that the second $a overrides the Please ignore that example. The important one is: use strict; I found this setting $x to undef counterintuitive, having learned that my $x = 1 if 1; Of course that's nothing technically wrong with the loop semantic, Hence the suggestion of a warning for this, or for a scope to be
Sneaky. That's easy to forget because it's not something one tries Unlike, say, C, in Perl if you try something and it works, and C<use In a language like C it's essential to keep in mind what is defined For example, in C evaluation order in expressions is quite flexible, Perl is much more deterministic, and this allows the language to be For example, C<my $timeout = 30 + (my $time = time())> is well These rules aren't obvious from the manual; they are learned by trying Point being that a mention in the manual isn't half as good as a End of rant. :)
What will the new semantics be? I find the current semantic of C<my $x = calculation if maybe_false> -- Jamie |
From @iabynOn Tue, Mar 09, 2004 at 06:51:47PM +0000, Jamie Lokier wrote:
my $x = foo if bar will be equivalent to: my $x; except that the scope of $x wont expand, eg in if (my $x = foo) ... $x still wont be visible outside the loop.
I doubt that it currently does what you expect: $ ./perl -le 'for (1..10) { my $x=10 if $_==1;;print $x; $x=20 }' 20 Most people find the printing of the value 20 unexpected. -- |
From @jlokierDave Mitchell wrote:
Oh. That's what I thought it was already!
You mean outside the conditional, I presume. By the way, this is inconsistent with the binding of regex results in Regexes matched in a loop have their result bound inside the loop, but while (/(.)/) { Yet conditionals bind regex results differently: if (/(.)/) { This binding for conditionals is very useful in practice, although I have occasionally changed an "if (/.../) {...}" to a "while (/.../)
Oh. I'm surprised too, and I actually use that construct in a few It looks quite similar to the regex bug with lexicals, #26909, which So perhaps the fix to this semantic will help with #26909? By the way, what happens if $x is assigned an object instead of 20? (Does a quick test). Yikes, the object isn't destroyed when its lexical scope if left $ perl -le 'package X; sub DESTROY { print "destroy" } finished Get rid of the conditional and it destroys the object when expected: $ perl -le 'package X; sub DESTROY { print "destroy" } destroy It's good news that it's to be fixed! Will that change fix the regex -- Jamie |
From @rgsJamie Lokier wrote in perl.perl5.porters :
The current plan is to fix it in 5.12 and deprecate it in 5.10.
Most strange ; I did reply to this bug, (mostly to say, "it's not a http://groups.google.com/groups?threadm=20040221225833.6d1dc31c.rgarciasuarez%40free.fr |
From @iabynOn Wed, Mar 10, 2004 at 04:41:39AM +0000, Jamie Lokier wrote:
Well, if (X){Y} deliberately has differnt scope semantics to Except... Its broken as regards my delcarations within the conditional: sub X::DESTROY { print "DESTROY\n" } which outputs: inner: X=ARRAY(0x817e000) There is a difference between compile-time and run-time scope here. This inconsistency smells like a bug to me. Which behaviour is wrong, Dave. -- |
Issues go stale after 60d of inactivity. |
Migrated from rt.perl.org#27517 (status was 'open')
Searchable as RT27517$
The text was updated successfully, but these errors were encountered: