Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl Data Types

Perl Data Types

Perl is a weakly typed language, so variables do not need a specified type; the Perl interpreter automatically selects the matching type based on context.

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

No. Type and description
1 Scalar

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

$myfirst=123;  #number 123 

$mysecond="123"; #string 123  | | 2 | arrays

Array variables begin with the character @ , with indexes starting at 0, such as:@arr=(1,2,3)

@arr=(1,2,3)

| | 3 | Hash

A hash is an unordered key/value pair collection. You can use a key as an index to get a value. Hash variables begin with the character % at the beginning.

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

|


Numeric literals

I. Integers

Perl actually stores integers in the floating-point registers of your computer, so they are effectively treated as floating-point numbers.

On most computers, a floating-point register can store about 16 digits; anything longer is discarded. Integers are actually a special case 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. For example:



$var1 = 047;    # 等于十进制的39

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

II. Floating-point numbers

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

Floating-point registers usually cannot store floating-point numbers precisely, resulting in errors, so special care is needed in operations and comparisons. The range of exponents is typically -309 to +308.

Example

#!/usr/bin/perl

$value

=

9.01

e

21

0.01

9.01

e

21

;

print

(

"

The first value is:

"

,

$value

,

"

\n

"

)

;

$value

=

9.01

e

21

9.01

e

21

0.01

;

print

(

"

The second value is:

"

,

$value

,

"

\n

"

)

;

Running the above program, the output is:



第一个值为:0

第二个值为:0.01

III. Strings

Strings in Perl are represented by a scalar, defined in a way similar to C, but in Perl strings are not terminated with \0.

The difference between double quotes and single quotes in Perl: double quotes can normally parse some escape characters and variables, while single quotes cannot parse them and output them as-is.

However, single quotes can be used to define multiline text, as shown below:



#!/usr/bin/perl 



$var='这是一个使用



多行字符串文本



的例子';



print($var);

Running the above program, the output is:



这是一个使用



多行字符串文本



的例子

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

Escape character Meaning
\ Backslash
' Single quote
" Double quote
\a System bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\0nn Create a number in octal format
\xnn Create a number in hexadecimal format
\cX Control character, x can be any character
\u Force the next character to uppercase
\l Force the next character to lowercase
\U Force all characters to be converted to uppercase
\L Force all characters to be converted to lowercase
\Q Add a backslash to non-word characters up to \E
\E End \L, \U, \Q

Example

Next, let's take a concrete look at the use of single quotes, double quotes, and escape characters:

Example

#!/usr/bin/perl

The newline \n inside double quotes is valid

$str

=

"

Runoob Tutorial

\n

www.runoob.com

"

;

print

"

$str

\n

"

;

The newline \n inside single quotes is invalid

$str

=

'

Runoob Tutorial \nwww.runoob.com

'

;

print

"

$str

\n

"

;

Only R is converted to uppercase

$str

=

"

\urunoob

"

;

print

"

$str

\n

"

;

All letters are converted to uppercase

$str

=

"

\Urunoob

"

;

print

"

$str

\n

"

;

The specified part is converted to uppercase

$str

=

"

Welcome to \Urunoob\E.com!

"

;

print

"

$str

\n

"

;

Add a backslash to non-word characters up to \E

$str

=

"

\QWelcome to runoob's family

"

;

print

"

$str

\n

"

;

The output of the above example is: