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" re-initialization failure #5321
Comments
From ken@nsds.comThis is a bug report for perl from ken@nsds.com, I have found that "my" does not re-initialize its variable to undef as The parameter list to my() may be assigned to if desired, This behavior only occurs under certain circumstances. Specifically, 0. The "my" declaration appears in a loop or subroutine. 1. The "my" declaration and initialization is followed by a my $value = "$initial_value\n" if defined $initial_value; 2. The declared value is "auto-initialized" later in the loop or $value .= "more text\n"; Here is a short construct to illustrate the bug: my $init = undef; Note that the initializer, $init, is undefined. In the first To work around the bug, I have found that separating the conditional my $value; Below is a script that illustrates the bug in other circumstances. #!/usr/bin/perl -w ################################################################################ use strict; # Illustrate bug using increment. my $counter = 0; # Illustrate bug using concatenation. # Illustrate bug using push. # Illustrate bug using hash assignment. # Illustrate bug using addition. ################################################################################ ################################################################################ ################################################################################ ################################################################################ ################################################################################ Flags: Site configuration information for perl v5.6.1: Configured by bod at Fri Jan 11 04:14:18 EST 2002. Summary of my perl5 (revision 5.0 version 6 subversion 1) configuration: Locally applied patches: @INC for perl v5.6.1: Environment for perl v5.6.1: |
From @schwernOn Thu, Apr 11, 2002 at 05:09:23PM -0700, Ken Neighbors wrote:
I think this is just a case of the surprising: my $foo if $bar; feature. Here's another way to think of it... $foo = 42; won't do what you mean for similar reasons. The module will be loaded Perhaps more illustrative: my $foo = 42; A simple fix would be... "don't do that". (This is an old and gnarled -- Michael G. Schwern <schwern@pobox.com> http://www.pobox.com/~schwern/ |
From @schwernOn Thu, Apr 11, 2002 at 07:01:14PM -0700, Ken Neighbors wrote:
Yes, but the "if defined $init" supresses the run-time effect, just for (1..10) { means: At compile-time, declare $foo. so this: for (1..10) { means: At compile-time, declare $foo it's just as if you'd basically said: { Perl is acting as expected. It's not a bug, just a misfeature. Because there's so much confusion around this construct, 5.8.0's NOTE: The behaviour of a "my" statement modified with a so the behavior of "my $foo if $bar" is officially undefined. -- Michael G. Schwern <schwern@pobox.com> http://www.pobox.com/~schwern/ |
From [Unknown Contact. See original ticket]Michael, Yes, my fix for this problem is "don't do that" anymore. However, I A "my" has both a compile-time and a run-time effect. At As you can see, it says "Actual initialization is delayed until run Note also that the problem only occurs when the variable is my $init = undef; Please run the above snippet and you'll see that the value is Anyway, I have now gone through much of my code and tried to find all my $value = $init if defined $init; In any event, I think that either this problem should be fixed or a BTW, thanks for the quick response! Ken Michael G Schwern writes:
|
From [Unknown Contact. See original ticket]Michael G Schwern writes:
Thank you for the extended explanation and the excerpt from the new Ken |
@smpeters - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#8930 (status was 'resolved')
Searchable as RT8930$
The text was updated successfully, but these errors were encountered: