Basically, a simple variable is a data unit, which can be a number or string.
I. Integer
1. Integer
The most common simple variables in Perl are not described here because they are basically the same as other languages.
Example:
$ X = 12345;
If (1217 + 116 = 1333 ){
# Statement block goes here
}
Integer restrictions:
Perl actually stores integers in floating-point registers on your computer, so they are actually treated as floating-point numbers. In most computers, floating-point registers can store about 16 digits, which are discarded. An integer is a special case of a floating point number.
2. octal and hexadecimal numbers
In hexadecimal notation, the headers are 0, and in hexadecimal notation, the headers are 0x.
Example: $ var1 = 047; (39 in decimal format)
$ Var2 = 0x1f; (31 in decimal format)
Ii. Floating Point Number
Such as 11.4,-0.3,. 3, 3., 54.1e + 02, 5.41e03
Floating-point registers usually do not store floating-point numbers accurately, resulting in errors. Pay special attention to them in operations and comparisons. The index ranges from-309 to + 308.
Example:
#! /Usr/local/bin/perl
$ Value = 9.01e + 21 + 0.01-9.01e + 21;
Print ("first value is", $ value, "\ n ");
$ Value = 9.01e + 21-9.01e + 21 + 0.01;
Print ("second value is", $ value, "\ n ");
---------------------------------------------------------
$ Program3_3
First value is 0
Second value is 0.01
Iii. String
CProgramNote that in Perl, the end of a string does not contain any implicit null character. The null character can appear anywhere in the string.
Strings in double quotation marks support replacement of simple variables, for example:
$ Number = 11;
$ Text = "This text contains the number $ number .";
The content of $ text is: "This text contains the number 11 ."
Character strings in double quotation marks support escape characters
Table 3.1. Escape sequences in strings.
Escape Sequence |
Description |
\ |
Bell (beep) |
\ B |
Backspace |
\ CN |
The CTRL +NCharacter |
\ E |
Escape |
\ E |
Ends the effect\ L,\ UOr\ Q |
\ F |
Form feed |
\ L |
Forces the next letter into lowercase |
\ L |
All following letters are lowercase |
\ N |
Newline |
\ R |
Carriage Return |
\ Q |
Do not look for special Pattern Characters |
\ T |
Tab |
\ U |
Force next letter into uppercase |
\ U |
All following letters are uppercase |
\ V |
Vertical Tab |
\ L, \ U, and \ q functions can be disabled by \ E, for example:
$ A = "t \ lhis is a \ estring"; # Same as "this is a string"
To include double quotation marks or backslash in a string, add a backslash before it. The backslash can also cancel variable replacement, for example:
$ Res = "A quote \" and a backslash \\";
$ Result = 14;
The result of print ("the value of \ $ result is $ result. \ n") is:
The value of $ result is 14.
. \ NNN (octal) or \ xnn (hexadecimal) can be used to represent ASCII characters, for example:
$ Result = "\ 377"; # This is the character 255, or EOF
$ Result = "\ xFF"; # This is also 255
. Single quotation mark string
There are two differences between a single quotation mark string and a double quotation mark string. One is that there is no variable replacement function, and the other is that the backslash does not support escape characters, but only takes effect when it contains single quotation marks and backslash. Another feature of single quotes is that it can span multiple rows, for example:
$ Text = 'this is two
Lines of text
';
It is equivalent to the following sentence:
$ Text = "this is two \ nlines of text \ n ";
. Mutual conversion between string and Value
Example 1:
$ String = "43 ";
$ Number = 28;
$ Result = $ string + $ number; # $ result = 71
If a string contains non-numeric characters, it starts from the left to the first non-numeric character, for example:
$ Result = "hello" * 5; # $ result = 0
$ Result = "12a34" + 1; # $ result = 13
. Initial variable value
In Perl, all simple variables have the default initial value: "", that is, null characters. However, it is recommended to assign initial values to all variables. Otherwise, when the program becomes large and complex, unexpected and difficult debugging errors may easily occur.