Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^4: No warning when assiging to a variable

by afoken (Abbot)
on Aug 20, 2013 at 18:54 UTC ( #1050223=note: print w/replies, xml ) Need Help??

in reply to Re^3: No warning when assiging to a variable
in thread No warning when assiging to a variable

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:

  • if ($var=anything) and while ($var=anything) should warn, because the assignment is probably unintentional, i.e. a possible bug.
  • if (($var=anything)) and while (($var=anything)) should not warn, because the assignment is marked as intentional by using extra parentheses.
  • if (my $var=anything) and while (my $var=anything) should not warn, because the assignment is marked as intentional (for use in the following block) by using my $var instead of a simple $var.
  • if (local $var=anything) and while (local $var=anything) should not warn, for the same reason.
  • if (our $var=anything) and while (our $var=anything) look really ugly. our does not belong there. But that's a different problem, so Perl should not warn, for the same reason as with my and local.

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.


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1050223]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2018-01-16 12:01 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (177 votes). Check out past polls.