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:
while(my $line = <>) { do_something($line); }

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:

if (($zero=0)) { do_something($zero); # never } if (my $one=1) { do_something($one); # always } while (($two=2)) { do_something($two); # for ever } while (my $empty='') { do_something($empty); # never }

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.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^4: No warning when assiging to a variable by afoken
in thread No warning when assiging to a variable by mikeraz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.