Perl data types

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

Perl has three basic data types: scalars, arrays, and hashes. The following is a description of the three data types:

serial number type and description
1 scalar

A scalar is one of the simplest data types in the Perl language. Variables of this data type can be numbers, strings, or floating-point numbers without strict distinction. When using, add a "$" in front of the variable name to indicate that it is a scalar. E.g:

$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. You can use the key as a subscript to get the value. Hash variables start with the characters "%".

%h=('a'=>1,'b'=>2); 

number literal

1. Integer

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 hold about 16 digits, and anything longer than that is discarded. Integers are special cases of floating-point numbers.

Integer variables and operations:

$x = 12345 ; if ( 1217 + 116 == 1333 ) { # execute code block } 
      
    

Octal and hexadecimal numbers: Octal starts with 0, hexadecimal starts with 0x. E.g:

$var1 = 047 ; # equals 39 decimal 
$var2 = 0x1f ; # equals 31 decimal         

2. Floating point numbers

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

Floating-point registers usually cannot accurately store floating-point numbers, resulting in errors. Special attention should be paid to operations and comparisons. The exponent typically ranges from -309 to +308.

example

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

执行以上程序,输出结果为:

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

三、字符串

Perl中的字符串使用一个标量来表示,定义方式和c很像,但是在Perl里面字符串不是用0来表示结束的。

Perl双引号和单引号的区别: 双引号可以正常解析一些转义字符与变量,而单引号无法解析会原样输出。

但是用单引号定义可以使用多行文本,如下所示:

#!/usr/bin/perl 

$var='这是一个使用

多行字符串文本

的例子';

print($var);

执行以上程序,输出结果为:

这是一个使用

多行字符串文本

的例子

Perl 语言中常用的一些转义字符如下表所示:

转义字符 含义
\\ 反斜线
\' 单引号
\" 双引号
\a 系统响铃
\b 退格
\f 换页符
\n 换行
\r 回车
\t 水平制表符
\v 垂直制表符
\0nn 创建八进制格式的数字
\xnn 创建十六进制格式的数字
\cX 控制字符,x可以是任何字符
\u 强制下一个字符为大写
\l 强制下一个字符为小写
\U 强制将所有字符转换为大写
\L 强制将所有的字符转换为小写
\Q 将到\E为止的非单词(non-word)字符加上反斜线
\E 结束\L、\U、\Q

实例

接下来让我们来具体看看单引号和双引号及转义字符的使用:

实例

#!/usr/bin/perl
# 换行 \n 位于双引号内,有效
$str = "菜鸟教程 \nwww.runoob.com";
print "$str\n";
# 换行 \n 位于单引号内,无效
$str = '菜鸟教程 \nwww.runoob.com';
print "$str\n";
# 只有 R 会转换为大写$str = "\urunoob";
print "$str\n";
# 所有的字母都会转换为大写$str = "\Urunoob";
print "$str\n";
# 指定部分会转换为大写$str = "Welcome to \Urunoob\E.com!";
print "$str\n";
# 将到\E为止的非单词(non-word)字符加上反斜线
$str = "\QWelcome to runoob's family";
print "$str\n";

以上实例执行输出结果为:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326054653&siteId=291194637