Perl data type

Perl is a weak type language, so variables do not need to specify a type, and the Perl interpreter automatically selects matching types based on context.

Perl has three basic data types: standard, array, hash. Here's a description of the three data types:

Perl data type

Digital literal

First, the whole type

PERL actually places 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 longer than this and are discarded. An integer is a special case of a floating point.

Integer variables and operations:

$x = 12345;
if (1217 + 116 == 1333) {
	# 执行代码语句块
}

8th and 16th: 8th starts at 0 and 16th starts at 0x. For example:

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

Second, floating points

Floating-point data such as: 11.4, -0.3, .3, 3. 、 54.1e+02 、 5.41e03。

Floating-point registers usually do not accurately store floating-point numbers, resulting in errors that should be paid special attention to in operations and comparisons. T he 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");

The above procedure is performed and the output is:

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

Third, the string

Strings in Perl are represented by a punctuation, which is defined in much the same way as c, but in Perl strings are not represented by 0.

The difference between Perl double quotes and single quotes: Double quotes resolve some escape characters and variables normally, while single quotes cannot resolve the output as is.

However, you can use multiple lines of text in single quotes, as follows:

#!/usr/bin/perl 

$var='这是一个使用

多行字符串文本

的例子';

print($var);

The above procedure is performed and the output is:

这是一个使用

多行字符串文本

的例子

Some of the escape characters commonly used in the Perl language are shown in the following table:

Escape characters Meaning
\\ Backslash
\' Single quotes
\" Double quotes
\a The system rings
\b Back off
\f Page break
\n Line change
\r Enter
\t Horizontal tabs
\v Vertical tabs
\0nn Create a number in octal format
\xnn Create a number in heteed format
\cX Control character, x can be any character
\u Force the next character to be capital
\l Force the next character to be lowercase
\U Force all characters to be converted to capitals
\L Force all characters to be converted to small case
\Q Adds a non-word (non-word) character to the backslash
\E Ends with .L., U., S.Q

Instance

Let's take a closer look at the use of single and double quotes and escape characters:

#!/usr/bin/perl

# 换行 \n 位于双引号内,有效
$str = "W3Cschool教程  \nwww.w3cschool.cn";
print "$str\n";

# 换行 \n 位于单引号内,无效
$str = 'W3Cschool教程  \nwww.w3cschool.cn';
print "$str\n";

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

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

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

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

The output of the above examples is:

Perl data type