Perl is a weakly typed language, so variables do not need to be typed; the Perl interpreter automatically selects a matching type based on context.
Perl has three basic data types: scalar, array, and hash. The following is a description of these three data types:
serial number | Type and description |
---|---|
1 | scalar Scalars are the simplest data type in Perl. Variables of this data type can be numbers, strings, floating point numbers, and no strict distinction is made. When using, add a "$" in front of the variable name to indicate it is a scalar. For example: $myfirst=123; #Number 123 $mysecond="123"; #String 123 |
2 | array Array variables start with the character "@" and the index starts from 0, such as: @arr=(1,2,3) @arr=(1,2,3) |
3 | Hash A hash is an unordered collection of key/value pairs. The value can be obtained using the key as a subscript. Hash variables start with the character "%". %h=('a'=>1,'b'=>2); |
PERL actually stores the integer in a floating-point register in your computer, so it is actually treated as a floating-point number.
In most computers, floating-point registers can store about 16 digits, and numbers longer than this are discarded. Integers are actually a special case of floating point numbers.
Integer variables and operations:
$x = 12345;if (1217 + 116 == 1333) { # Execute code statement block}
Octal and hexadecimal numbers: Octal numbers start with 0, and hexadecimal numbers start with 0x. For example:
$var1 = 047; # equal to 39 in decimal $var2 = 0x1f; # equal to 31 in decimal
Floating point data such as: 11.4, -0.3, .3, 3., 54.1e+02, 5.41e03.
Floating-point registers usually cannot store floating-point numbers accurately, resulting in errors. Special attention should be paid to operations and comparisons. The index usually ranges from -309 to +308.
Executing the above program, the output result is:
The first value is: 0 The second value is: 0.01
Strings in Perl are represented by a scalar, and the definition method is very similar to that in C. However, strings in Perl do not end with 0.
The difference between Perl double quotes and single quotes: Double quotes can parse some escape characters and variables normally, while single quotes cannot be parsed and will be output as they are.
But multiple lines of text can be used when defined with single quotes, like this:
#!/usr/bin/perl $var='This is an example of using multi-line string text';print($var);
Executing the above program, the output result is:
Here is an example using a multi-line string literal
Some commonly used escape characters in the Perl language are shown in the following table:
escape character | meaning |
---|---|
\ | backslash |
' | single quote |
" | double quotes |
a | System rings |
b | Backspace |
f | form feed |
n | newline |
r | Enter |
t | horizontal tab |
v | vertical tab |
nn | Create a number in octal format |
xnn | Create a number in hexadecimal format |
cX | Control character, x can be any character |
u | Force next character to be uppercase |
l | Force next character to be lowercase |
U | Force all characters to uppercase |
L | Force all characters to lowercase |
Q | Add backslash to non-word characters up to E |
E | End L, U, Q |
Next, let's take a closer look at the use of single quotes, double quotes and escape characters: