|laziness, impatience, and hubris|
Re^4: No warning when assiging to a variableby afoken (Prior)
|on Aug 20, 2013 at 18:54 UTC||Need Help??|
Perl is not C.
No, but it borrows several ideas and a lot of syntax from C.
Let's look at a common idiom in Perl:
Yes, this is a common idiom, but it differs from a simple, probably accidental assignment by introducing my. I would consider this as a clear marker for an intentional assignment, like the semi-redundant parentheses that gcc uses.
So, the rules for assignment-in-condition warnings should probably be as following:
Of course, the same rules would apply to elsif, unless and until; and of course also to assignments to arrays and hashes.
IMHO, it proves: Perl is not C, C is not Perl. C is a compiled language. Perl is a interpreted language.
And it would not hurt to adopt the idea of testing for unintentional assignments. Perl already has code to check for unintentional assignments of constants, all that would be needed is to expand it to check for unintentional assignments of anything else. This is what gcc does, this is what mikeraz expects, and this is what I consider a good thing.
In my opinion, the above Perl idiom should not emit a warning like C does.
It would not, because of my. Adding semi-redundant parentheses (i.e. while (($line=<>))) would silence the warning, too.
The similar while (<>) implicitly assigns to $_, and this assignment is intentional, so no warning here.
My little set of rules still allows stupid code like the following:
It seems the existing check for a constant assignment, intentional or not, is still useful.
That check should come first. Only if that check does not issue a warning, Perl should check for the intention markers (semi-redundant parentheses, my, local, our) around an assignment, and issue a similar warning if they are missing.
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)