Perl data type


Release date:2023-10-11 Update date:2023-10-21 Editor:admin View counts:207

Label:

Perl data type

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

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

Serial number

Type and description

1

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

$myfirst=123; #number123 

$mysecond= “123”; #string 123

2

Array array variables start with the character @, and the index starts with 0, such as: @ arr= (1pm 2p3)

@arr=(1,2,3)

3

A hash is an unordered collection of key/value pairs. You can use the key asthe subscript to get the value. The hash variable begins with the character%.

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

Digital literal quantity

I.Integer

Perl actually stores integers in floating-point registers on your computer, so it is actually treated as floating-point numbers.

In most computers, floating-point registers can store about 16 digits, and longer ones are discarded. Integers are a special case of floating-point numbers.

Integer variables and operations:

$x = 12345;
if (1217 + 116 == 1333) {
    # Execute Code Statement Block
}

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

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

2.Floating point numbers

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

Floating-point registers usually cannot store floating-point numbers accurately, resulting in errors, which should be paid special attention to in operation and comparison. The index usually ranges from-309 to + 308.

Example

#!/usr/bin/perl$value=9.01e+21+0.01-9.01e+21;print("The first value is:",
$value,"\\n");$value=9.01e+21-9.01e+21+0.01;print("The second value is:",$value,"\\n");

Execute the above program, and the output is as follows:

The first value is: 0
The second value is: 0.01

3. String

The string in Perl is represented by a scalar, which is defined much like c,but the string does not end with \ 0 in Perl.

The difference between Perl double quotes and single quotes: double quotes can normally parse some escaped characters and variables, while single quotes cannot be parsed and output as is.

However, multiple lines of text can be used in single quotation marks, as follows:

#/ Usr/bin/perl
$var='This is a function that uses
Multiline String Text
Example of ';
Print ($var);

Execute the above program, and the output is as follows:

This is a usage
Multiline String Text
Example

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

Escape character

Meaning

\\

Backslash

\'

Single quotation mark

\"

Double quotation marks

\a

The system rings.

\b

Backspace

\f

Feed character

\n

New line

\r

Enter

\t

Horizontal tab character

\v

Vertical tab character

\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 be uppercase

\l

Force the next character to be lowercase

\U

Force all characters to uppercase

\L

Force all characters to be lowercase

\Q

Backslash non-word (non-word) characters up toE

\E

End L,U,Q

Example

Let’s take a specific look at the use of single and double quotes and escapecharacters:

Example

#/ Usr/bin/perl # Wrap n
Within double quotes, valid $str="Rookie Tutorial nwwww. runoob. com"; Print "$str n"# Wrap
\\N is within single quotes, invalid $str='Rookie Tutorial nwwww. runoob. com'; Print "$str n"#
Only R will be converted to uppercase $str=" urunoob"; Print "$str n"#
All letters will be converted to uppercase $str=" Urunoob"; Print "$str n"#
The specified part will be converted to uppercase $str="Welcome to Urunoob E.com!"; Print "$str n"#
Add a backslash $str=" QWelcome to runoob's" to non word characters up to E
Family "; print" $str n ";

The output of the above example is as follows:

Image0

Powered by TorCMS (https://github.com/bukun/TorCMS).