From the course: Perl 5 Essential Training
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Compound assignment operators - Perl Tutorial
From the course: Perl 5 Essential Training
Compound assignment operators
- [Voiceover] The compound assignment operators are a nice convenience feature that was borrowed from the C language. Here is a working copy of hello.pl from chapter eight of the exercise files. For example, if I come down here and I create a variable, and assign it a value, so I now I've got $x is equal to seven. And let's say that I want to add two to this. I could say, $x equals $x plus two, and say $x, and I'll get nine, and that's correct. Or I could use the compound assignment operator, I can take this assignment and the addition and put them in one operator, and say, plus equals, x plus equals two. And you notice I get exactly the same result. So this both adds and assigns in one operator. So that's why it's called the compound assignment operator. It has the side effect of only evaluating $x once, and there are circumstances where that may matter. In other words, if I were doing this in two steps like this, it actually evaluates $x twice. And if that were a class or something…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.