Perl (Scripting) - Addition and Increment (+)

by
Jeremy Canfield |
Updated: August 04 2022
| Perl (Scripting) articles
Here is the most basic example of how to add two numbers in Perl.
#!/usr/bin/perl
use strict;
use warnings;
print (1 + 1);
It is much more common to store the result in a variable.
#!/usr/bin/perl
use strict;
use warnings;
my $sum = (1 + 1);
print "$sum \n";
Which should return the following.
2
Increment
++ can be used to increment a value by one.
my $value = 1;
++$value;
print "$value \n";
Which should return the following.
2
Appending
+= can be used to add to an already set value.
$i = 1;
$i += 2;
print "$i \n";
Which should return the following.
3
Decimal numbers
By default, decimal will be used.
my $sum = (1.1 + 1.1);
print "$sum \n";
Which should return the following.
2.2
Whole numbers
The integer module can be used to round to whole numbers.
#!/usr/bin/perl
use strict;
use warnings;
use integer;
my $sum = (1.1 + 1.1);
print "$sum \n";
Which should return the following.
2
Did you find this article helpful?
If so, consider buying me a coffee over at