Latest web development tutorials

Perl Data Types

Perl is a weakly typed language, so no need to specify the type of a variable, Perl interpreter will automatically select the match type depending on the context.

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

No. Type and Description
1 Scalar quantity

Scalar Perl language is the simplest type of data. Variable of this data type can be numbers, strings, floats, does not make a strict distinction. When using the variable name in front with a "$" to indicate a scalar. E.g:

$myfirst=123;     #数字123 

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

Array variable with the character "@" at the beginning, the index from zero, ie: @ arr = (1,2,3)

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

Hash is an unordered key / value pairs. You can use the key as a subscript to get the value. Hash Variables with the character "%" at the beginning.

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

Numeric literals

A, int

PERL fact integer stored in your computer's floating-point registers, so in fact be viewed as a floating-point number.

In most computers, floating-point registers can be stored about 16 numbers longer than this are discarded. Real integer to floating-point exceptions.

Integer variables and arithmetic:

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

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

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

Second, float

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

Floating-point registers are usually not accurately storing floating-point, resulting in errors in operation and pay special attention to the comparison. Index range is generally from -309 to +308. E.g:

#!/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 program, the output is:

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

Third, the string

Perl string is represented using a scalar, and c define the way like, but not with strings in Perl inside 0 to indicate the end of.

The difference between Perl double quotes and single quotes: double quotes can normally resolve some of the escape character variable, while the single quotation marks will not be parsed as output.

However, you can use single quotes to define multi-line text, as follows:

#!/usr/bin/perl 

$var='这是一个使用

多行字符串文本

的例子';

print($var);

The above program, the output is:

这是一个使用

多行字符串文本

的例子

Perl language commonly used in some of the escape character in the following table:

Escape character meaning
\\ Backslash
\ ' apostrophe
\ " Double quotes
\ A System Bell
\ B Backspace
\ F Page breaks
\ N Wrap
\ R Enter
\ T Horizontal tab
\ V Vertical tab
\ 0nn Create octal digital format
\ Xnn Create a hexadecimal number format
\ CX Control characters, x can be any character
\ U Forcing the next character to uppercase
\ L Forced next character to lowercase
\ U Forces all characters to uppercase
\ L Forces all characters to lowercase
\ Q We will go to \ E until the non-word (non-word) with a backslash character
\ E End \ L, \ U, \ Q

Examples

Let's look at the specific use of single and double quotation marks and escape characters:

#!/usr/bin/perl

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

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

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

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

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

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

Examples of the implementation of the above output is: