You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Two days ago I spent quite some time debugging this code:
# a lot of codemy$num = 0;
# more codesubtest {
# complex functionreturn (my$num = shift) ? $num : 0;
}
# ...say test 4; # 0# ...
When I found the bug I initially thought that the issue was related to the ternary operator. But after creating an issue (#17795) I learned that you cannot use variables on the same expression they are declared, excerpt shared by @Grinnz from the documentation:
The declared variable is not introduced (is not visible) until after the current statement.
Currently these will generate a compilation error under strict (Global symbol "$n" requires explicit package name) and a warning under warnings (Name "main::n" used only once: possible typo):
But this will only generate a compilation error under strict (nothing with warnings):
((my$n) || !!$n) ? $n : 0; # Perl thinks it's fine when not using strict
Anyways, if you do something like the code I showed on the first example, Perl won't warn you nor generate a compilation error and will probably do what you did not mean to do:
caribpa
changed the title
[feature] Warn when using a variable on the same statement it is locally masked
Warn when using a variable on the same statement it is locally masked
May 21, 2020
Two days ago I spent quite some time debugging this code:
When I found the bug I initially thought that the issue was related to the ternary operator. But after creating an issue (#17795) I learned that you cannot use variables on the same expression they are declared, excerpt shared by @Grinnz from the documentation:
Currently these will generate a compilation error under
strict
(Global symbol "$n" requires explicit package name
) and a warning underwarnings
(Name "main::n" used only once: possible typo
):But this will only generate a compilation error under
strict
(nothing withwarnings
):Anyways, if you do something like the code I showed on the first example, Perl won't warn you nor generate a compilation error and will probably do what you did not mean to do:
and I can say first hand that finding this bug is something really time-consuming with no pragma nor Deparse to help you.
I'd like to see a warning here saying that you are using a variable on the same statement you are lexically masking it.
Though I see that this warning would also appear on the likely more common case:
The text was updated successfully, but these errors were encountered: