Perl data types


Perl is a weakly typed language, so variables do not need to be specified. The Perl interpreter will automatically select a matching type based on the context.

Perl has three basic data types: scalar, array, and hash. The following is a description of these three data types:

Serial numberType and description
1Scalar

Scalar is the simplest data type in the Perl language. 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 that it is a scalar. For example:

$myfirst=123;     #数字123 

$mysecond="123";   #字符串123 
2Array

The array variable starts with the character "@" and the index starts from 0, such as :@arr=(1,2,3)

@arr=(1,2,3)
3Hash

The hash is a None An ordered 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);

Number literals

1. Integers

PERL actually stores integers in floating-point registers in your computer, so they are actually treated as floating-point numbers. .

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) {
	# 执行代码语句块
}

Octal and hexadecimal numbers: Octal starts with 0, and hexadecimal starts with 0x. For example:

$var1 = 047;    # 等于十进制的39
$var2 = 0x1f;   # 等于十进制的31

2. Floating point numbers

Floating point number 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. For example:

#!/usr/bin/perl 

$value = 9.01e+21 + 0.01 - 9.01e+21;
print ("第一个值为:", $value, "\n");
$value = 9.01e+21 - 9.01e+21 + 0.01;
print ("第二个值为:", $value, "\n");

Execute the above program, the output result is:

第一个值为:0
第二个值为:0.01

3. String

The string in Perl is represented by a scalar, and the definition method is very similar to that in c Like, but in Perl, strings 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 is.

But you can use multi-line text defined with single quotes, as shown below:

#!/usr/bin/perl 

$var='这是一个使用

多行字符串文本

的例子';

print($var);

Execute the above program, the output result is:

这是一个使用

多行字符串文本

的例子

Some conversions commonly used in Perl language The meaning characters are as shown in the following table:

Escape character Meaning
\\Backslash
\'Single quote
\"Double quote
\aSystem Ring
\bBackspace
\fForm feed
\nLine break
\rEnter
\tHorizontal tab
\vVertical Tab
\0nnCreate numbers in octal format
\xnn Create a number in hexadecimal format
\cX control character, x can be any character
\u Force the next character to be uppercase
\lForce the next character to be lowercase
\UForce all characters to be converted to uppercase
\LForce all characters to be converted to lowercase
\QAdd backslashes to non-word characters up to \E
\EEnd \L, \U, \Q
##Example

Let’s take a closer look at single quotes, double quotes and escape characters Use:

#!/usr/bin/perl

# 换行 \n 位于双引号内,有效
$str = "php中文网  \nwww.php.cn";
print "$str\n";

# 换行 \n 位于单引号内,无效
$str = 'php中文网  \nwww.php.cn';
print "$str\n";

# 只有 R 会转换为大写
$str = "\uphp";
print "$str\n";

# 所有的字母都会转换为大写
$str = "\Uphp";
print "$str\n";

# 指定部分会转换为大写
$str = "Welcome to \Uphp\E.com!"; 
print "$str\n";

# 将到\E为止的非单词(non-word)字符加上反斜线
$str = "\QWelcome to php's family";
print "$str\n";

The output result of the above example execution is: