Perl: More on Data Types and Operators - Operating the Deep Seas
(Page 3 of 5 )
Argh! Thar she blows! The great white whale of the sea -- or, more precisely, the treasure that every Perl diver seeks. In this case, though, it's not something that goes into jewelry, it's something that adds quite a bit of power to your coding.
Operators in Perl (as in all programming languages) allow you to manipulate data. You've worked with operators ever since elementary school, albeit somewhat differently.
Just as there are different data types, there are different operators for the different kinds of data. We will go through a bunch of them. First though, how would you like to see a gigantic table full of mind-numbing data? I thought you might.
Mathematical Operators
Symbol | What it Does |
+ | Adding |
- | Subtracting |
* | Multiplying |
/ | Dividing |
% | Modulating |
** | Exponentiating |
The above operators work just as you think they would. If I wanted to figure out my weekly salary, I could do the follow code:
#!/usr/local/bin/perl
$hours = 40;
$wage = 20;
$total_salary = $hours * $wage;
print $total_salary
That would give me the following result:
800
Operators that Assign Values
| | Symbol | What it Does |
= | Assign Normally |
+= | Assign and Add |
-= | Assign and Subtract |
*= | Assign and Multiply |
/= | Assign and Divide |
%= | Assign and Modulate |
**= | Assign and Exponentiate |
In the previous tutorial we used the = symbol to assign a value to a variable. We can use it for other things as well. For instance, let's say you are the boss's son and have just received your first paycheck. It isn't enough to buy that Porsche you desperately need to compensate for certain characteristics you lack. Well, you can always change it like this:
$pay_rate = 20;
$pay_rate+=80;
print $pay_rate;
The above example would print your pay rate as being: 100. Another way of looking at it is saying you had written this code instead:
$pay_rate = 20;
$pay_rate=$pay_rate + 80;
print $pay_rate;
You can do the same sort of thing with the other operators above.
Next: Incremental/ Decremental >>
More Perl Articles
More By James Payne