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
"my" variable appears to inadvertently act as "state" variable #15764
Comments
From dcmertens.perl@gmail.comCreated by dcmertens.perl@gmail.comFor this script: for (1 .. 6) { I would expect $thing to start out each time through the loop as 1) thing is bar However, I get 1) thing is bar Somehow, $thing accumulates data with each iteration of the loop, but Perl Info
|
From @tonycozOn Mon, 12 Dec 2016 07:23:24 -0800, run4flat wrote:
This (mis-)behaviour is well known and documented to be changable, B<NOTE:> The behaviour of a C<my>, C<state>, or It's retained for compatibility with older code which used this to my $x if 0; but this specific usage now generates a warning. $ ./perl -e 'my $x if 0' Perhaps the more general case should also produce a warning. Tony |
The RT System itself - Status changed from 'new' to 'open' |
From dcmertens.perl@gmail.comI came upon this behavior completely unexpectedly. Would it be feasible to David On Tue, Dec 13, 2016 at 11:41 PM, Tony Cook via RT <
-- |
From @wolfsageOn Wed, Dec 14, 2016 at 9:08 AM, David Mertens <dcmertens.perl@gmail.com> wrote:
This is something I have been working on (slowly). It would go faster But otherwise you can bury variable declarations deep in the op tree I think I've figured out what needs doing but I don't see having it in -- Matthew Horsfall (alh) |
From @xsawyerxOn 12/14/2016 06:05 PM, Matthew Horsfall (alh) wrote:
Iterations FTW? Seriously though, nothing wrong with a start that catches some, if not all. |
From @iabynOn Thu, Dec 15, 2016 at 01:21:52PM +0100, Sawyer X wrote:
There's some history to 'my $x if ...' deprecations. Back in 2004, when I were a lad, there was this thread: http://nntp.perl.org/group/perl.perl5.porters/88428 which led to these commits of mine: perl-5.8.0-3308-g76df5e8 remove C<my $x if foo> construct from Then there was a later thread, http://nntp.perl.org/group/perl.perl5.porters/89114 which decided that the general deprecation was wrong (and the perl-5.8.0-3389-g722969e retract 22328 and 22332: deprecation I can't find any discussion as to why the first set of patches was such a $cond = ...; Also, we may need to distinguish between my $x if $cond; We may want to deprecate one but not the other. -- |
From @iabynOn Mon, Mar 27, 2017 at 06:17:46PM +0100, Dave Mitchell wrote:
I've now found the thread: http://nntp.perl.org/group/perl.perl5.porters/88928 Apparently it broke everything. -- |
From @iabynOn Tue, Mar 28, 2017 at 01:03:02AM +0100, Dave Mitchell wrote:
Also see -- |
Hi! I know about weird and hard-to-fix Perl behavior with conditional 'my' and similar declarants. But there is at least one more situation when 'my' variable turns into 'state' variable, and without condition for 'my'. I tried to search for a bug report, but failed. This situation is actual for version 5.38 (not tested over current 5.39). It's GOTO which bypasses 'my' declaration. I know that C-compilers always warn or even error when "goto bypasses initialization of local variable", Perl does not. At least with default settings. use strict; Outputs: Expected: some warning or error about bypassing lexical var declaration So, everytime when $data is assigned inside "if(!$data)" after bypassing "my $data", it becomes a 'state' variable until "my $data" is reached during some next iteration. Situation is completely the same as "my in false condition" and has the same cause, as I understand, but is somewhat more difficult to be found. And, I guess, not documented. |
On Fri, Mar 15, 2024 at 06:27:19PM -0700, Yuriy Yevtukhov wrote:
I know all the rules like 'do not use goto....blah blah', there are some
in documentation in particular, but this specific situation of using
GOTO in a "C-style" within single function... would be great to have a
warning or even error if lexical var is used after its
declaration/initialization was bypassed by GOTO.
I'm not opposed to adding such a warning (or even making in it an error
within the scope of a suitable 'use v5.xx'), but I suspect adding such a
warning at compile time might be tricky in terms of false
positives/negatives.
…--
"You're so sadly neglected, and often ignored.
A poor second to Belgium, When going abroad."
-- Monty Python, "Finland"
|
Migrated from rt.perl.org#130328 (status was 'open')
Searchable as RT130328$
The text was updated successfully, but these errors were encountered: