# Perl ABC

1. Perl
2. Operators
3. here

## Perl arithmetic operators

Describes the arithmetic operators for performing four arithmetic operations in Perl.

my \$add = 1 + 1;

#### Subtraction

Use subtraction operator "-" to do the subtraction.

# Subtraction
my \$sub = 9 - 1;

#### Multiplication

To do the multiplication, use multiplication operator "*".

# Multiplication
my \$multi = 5 * 4;

#### Division

Use division operator "/" to perform the division.

# Division
my \$div = 9/4;

The answer to division is a decimal if it is not divisible.

### Four rules of operation with a simple description

If you want to perform 4 rules on yourself, there is a simple description method.

# Same as \$sub = \$sub -5
\$sub-= 5;

# Same as \$multi = \$multi * 2
\$multi *= 2;

# Same as \$div = \$div/7
\$div/= 7;

### Seeking quotient and remainder

There is no operator to find the quotient. After dividing with the/operator, the int function retrieves the integer part.

# Quotient
my \$div_int = int(9/4);

To find the remainder, use modulo operator.

# Remainder
my \$mod = 9% 4;

### Find exponentiation

Use the ** operator to find the power.

my \$two_power_num = 4 ** 2;

Use exponentiation operator "**" to find the exponentiation. Exponentiation is an operation that multiplies the same number multiple times.

### Increment and decrement operators

Increment Operator and decrement operator. A special description is provided. You can add 1 to the description ++ \$i or ++ \$i, and subtract 1 from the description - \$i or \$i- .

# Increment
\$num ++;

# Decrement
\$num- ;

There are two ways to write increments and decrements, depending on whether you put ++ or-before or after the variable. Note that the meaning changes depending on the context, depending on whether the operator is added before or after the variable.

my \$i = 0;
my \$k = ++ \$i;
my \$i = 0;
my \$k = \$i++;

The above two have different values in \$k. In the first case, \$k contains 1. In the second case, \$k will be 0.

If you put ++ before, \$i is added 1 before the assignment, and if you put ++ after it, 1 is added to \$i after the assignment.

I explained in the case of assignment, but I will also give an example of passing a value to a function.

# The argument of func is \$i plus one.
func (++ \$i);

# \$I is passed to the argument of func, and then 1 is added.
func (\$i++);

### 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.

### 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.

### Example program

This is an example program for numerical calculation.

#### Four arithmetic operations

Examples of addition, subtraction, multiplication, and division.

use strict;
use warnings;

print "(1) 4 rules operation\n";

my \$add = 1 + 1;

# Subtraction
my \$sub = 9 - 1;

# Multiplication
my \$multi = 5 * 4;

# Division
my \$div = 9/4;

print "\\$sub = \$sub\n";
print "\\$multi = \$multi\n";
print "\\$div = \$div\n\n";

print "(2) Four-rule operation with a simple description\n";

# Same as \$sub = \$sub -5
\$sub-= 5;

# Same as \$multi = \$multi * 2
\$multi *= 2;

# Same as \$div = \$div/7
\$div/= 7;

print "\\$sub = \$sub\n";
print "\\$multi = \$multi\n";
print "\\$div = \$div\n\n";

print "(3) Operation to find quotient and remainder\n";
# Quotient
my \$div_int = int(9/4);
# Remainder
my \$mod = 9% 4;

print "\\$div_int = \$div_int\n";
print "\\$mod = \$mod\n";

#### Find exponentiation

This is an example to find the power.

use strict;
use warnings;

# Find a power.
print "1: Find the power.\n";
my \$num = 4;
my \$two_power_num = 4 ** 2;
print "The square of \$num is \$two_power_num.\n";

#### Increment and decrement

This is an example using the increment operator and the decrement operator.

use strict;
use warnings;

# 1 is added before being evaluated.
print "(1) Prefix increment and decrement\n";
my \$i = 0;
print ++ \$i . "\n";
print \$i . "\n";

print-\$i . "\n";
print \$i . "\n";

# 1 is added after being evaluated.
print "(2) Post-increment and decrement\n";
\$i = 0;
print \$i++ . "\n";
print \$i . "\n";

print \$i- . "\n";
print \$i;