-6

Could someone help me to understand what the '+=' operator means in a particular situation. The script says:

$receipts{$weather} += $receipt;
$days{$weather}++;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
joesh
  • 171
  • 3
  • 9
  • 6
    You couldn't find the answer to this elsewhere? – Tyler MacDonell Mar 22 '13 at 18:15
  • 1
    See [What does the `|=` operator mean in C++](http://stackoverflow.com/questions/4217762/what-does-the-operator-mean-in-c). The notation was borrowed by C++ (and Perl, and Java, and ...) from C. There's a discussion there of why the assignment operators like `+=` are beneficial because they avoid a particular class of mistake. – Jonathan Leffler Mar 22 '13 at 18:27
  • Hi Tyler M. Actually it is explained in the Perl Introduction book, but not so clearly. It talks more about left/right priorities. I just wanted a little help clarifying it here. A good source of helpful people. – joesh Mar 23 '13 at 11:37

4 Answers4

11

Assuming $foo += $bar, the += operator does the following:

$foo = $foo + $bar;

That is, increments $foo by $bar. Assuming $foo++, the ++ operator does the following:

$foo = $foo + 1;

That is, increments the variable by one.

With all this said, these operators also have some hidden perl magic. For example, the += and ++ operator does not give an uninitialized warning where the corresponding statement would:

# $foo is undefined
$foo += 10;        # no warning
$foo++;            # no warning
$foo = $foo + 10   # Use of uninitialized value $foo in addition

The ++ operator also works on strings

my $foo = 'a';
$foo++;
print $foo;     # prints 'b'

The ++ operator comes in two flavours, post increment and pre increment. The return value of the expression is either calculated before or after the incrementation:

$foo = 1;
print ++$foo;   # prints 2
print $foo++;   # prints 2, but $foo is now 3
TLP
  • 66,756
  • 10
  • 92
  • 149
  • @Birei It is actually documented. `The auto-decrement operator is not magical.` in [perldoc perlop](http://perldoc.perl.org/perlop.html#Auto-increment-and-Auto-decrement) – TLP Mar 22 '13 at 19:01
  • `EXRP1 += EXRP2;` is not quite equivalent to `EXRP1 = EXRP1 + EXRP2;`. The former only evaluates `EXPR1` once. This matters for magical vars and lvalue subs. Same goes for `EXPR1++;`. – ikegami Mar 22 '13 at 19:51
  • @ikegami The documentation said it was equivalent, not me. :P – TLP Mar 22 '13 at 23:15
  • You should read again. "Assignment operators work as in C. That is, `$a += 2;` is equivalent to `$a = $a + 2;` although without duplicating any side effects that dereferencing the lvalue might trigger, such as from tie()." – ikegami Mar 22 '13 at 23:35
  • @ikegami Yes. The point was that I didn't use the word "equivalent", because that's overstating. – TLP Mar 23 '13 at 00:31
  • I know. My first comment qualified your post. My second comment contradicted your claim that the docs said they were equivalent. – ikegami Mar 23 '13 at 00:42
  • Great, thanks. Actually I forgot to leave out the ++ line, I cut and pasted it in so that the context for the line above was clearer. I knew that ++ is the same as + 1. – joesh Mar 23 '13 at 11:39
  • @user2095020 You're welcome. If you think this answer answers your question, you can accept it by clicking the check mark next to it. You should do that with all your questions, in fact. – TLP Mar 23 '13 at 11:43
3

It is adding the value of $receipt to the value of $receipts{$weather} and storing the result back into $receipts{$weather}. It is the equivalent of:

$receipts{$weather} = $receipts{$weather} + $receipt

However, it may be implemented more efficiently in some cases.

isedev
  • 18,848
  • 3
  • 60
  • 59
  • That's great, thanks. So actually the use of the curly brackets '{}' is a way of using two scalers/variables together, if I understand your explanation correctly. That's clear now, thanks. I guessed that was the case, in the Perl book it's explained, but not so clearly. – joesh Mar 23 '13 at 11:44
2

See perldoc perlop:

"=" is the ordinary assignment operator.

Assignment operators work as in C. That is,

   $a += 2;

is equivalent to

   $a = $a + 2;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • Thanks, as I said above it's now clear. It is explained in the Perl book, but never 'so' clearly. Thanks again. – joesh Mar 23 '13 at 11:45
0

Example:

this example : int i = 2; i=i+4; and this example int i = 2; i+=4 are the same;

Nathan
  • 24,586
  • 4
  • 27
  • 36
  • 2
    That looks more like C than Perl. `my $i = 2; $i = $i + 4;` and `my $i = 2; $i += 4;` would be more compelling. – Jonathan Leffler Mar 22 '13 at 18:20
  • Thanks for the answer, it looks like I came to the right place. As always everybody is always really helpful. Thanks, it seems the questions been answered 4 times. – joesh Mar 23 '13 at 11:47