- Perl ›
- Operators ›
- Arithmetic operator ›
- here
Addition operator "+" - Addition of numbers
You can use the addition operator "+" to add numbers.
my $num3 = $num1 + $num2;
This is an example that adds 3 and 5.
my $total = 3 + 5;
In Perl, even a small number of additions can be done without being aware of the type.
my $total = 3.5 + 2.7;
Numeric automatic expansion
Perl does not have a numeric type in programming languages, but it does have a numeric type internally.
If it cannot be calculated with a 32-bit integer, it will be extended to a 64-bit integer and calculated if the environment supports it.
If it cannot be represented by a 64-bit integer, it will be expanded to a double-precision floating-point type and calculated.
String concatenation is done with the string concatenation operator
In Perl, the operator "+" is used only to add numbers. If you want to concatenate strings, use string concatenation operator ".".
my $str = 'ABC'.'DEF';
Addition of strings can be done as it is
Perl automatically converts strings to numbers, and even strings can be added with the addition operator if they contain numbers. You can add.
my $num_str1 = "34"; my $num_str2 = "56"; my $total = $num_str1 + $num_str2;
Perl is extremely useful for small tasks because you can get a string containing numbers from a text file and add it as is.
Explanation of arithmetic operators
The addition operator is one of the arithmetic operators. For a detailed explanation of arithmetic operators, see the Arithmetic Operators Explanation page.