Perl data type

Perl is a weakly typed language, so variables don't need to specify a type, and the Perl interpreter automatically chooses the match type based on the context.

Perl has three basic data types: scalars, arrays, and hashes. The following are descriptions of these three data types:

Order Type and Description
1 scalar

The scalar is the simplest data type in the Perl language. Variables of this data type can be numbers, strings, and floating point numbers, without strict distinction. Use a $ in front of the variable name when it is used, indicating that it is a scalar. For example:

$myfirst=123; #number123 

$mysecond="123"; #string123 
2 array

Array variables begin with the characters @ and the index starts at 0, such as: @arr=(1,2,3)

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

Hash is an unordered key/value pair of collections. You can use the key as a subscript to get the value. Hash variables begin with the characters %.


Digital literal

First Integer

Perl actually stores integers in floating-point registers in your computer, so it's actually treated as a floating-point number.

In most computers, the floating point register can store about 16 digits, which is discarded. Integers are special cases of floating point numbers.

Integer variables and operations:

$x = 12345;
if ( 1217 +  116 == 1333) {
    # Execution of the code statement block

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

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

Second, floating point number

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

Floating point registers usually do not store floating point numbers accurately, resulting in errors, and special care should be taken in operations and comparisons. The index usually ranges from -309 to +308.


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

Execute the above program, the output is:

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

Third, string

Strings in Perl are represented by a scalar, defined in much the same way as c, but in Perl the string does not end with \0.

The difference between Perl double quotes and single quotes: Double quotes can parse some escape characters and variables normally, and single quotes can't be parsed and output as they are.

But with single quotes, you can use multiple lines of text, as follows:


$var='This is a use

Multi-line string text

 Example ';


Execute the above program, the output is:

This is a use

Multi-line string text


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

escape character meaning
\\ backslash
\' single quotes
\" double quotes
\a System Ring
\b Backspace
\f page break
\n Line break
\r Enter
\t Horizontal tabs
\v Vertical Tabs
\0nn Create a number in octal format
\xnn Create a number in hexadecimal format
\cX control character, x can be any character
\u Forcing the next character to uppercase
\l Forcing the next character to lowercase
\U Forcing all characters to uppercase
\L Forcing all characters to lowercase
\Q Add non-word characters to \E plus backslashes
\E End \L, \U, \Q


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


#!/usr/bin/perl # Line breaks \n in double quotes, valid $str = " Welookups Tutorial\nwww.welookups.com"; print "$str\n"; # Newline \n is in single quotes, not valid $str= ' Welookups Tutorial\nwww.welookups.com'; print "$str\n"; # Only R will be converted to uppercase $str = " \uwelookups"; print "$str\n"; # All letters are converted to uppercase $str = " \Uwelookups"; print "$str\n"; # The specified part will be converted to uppercase $str = " Welcome to \Uwelookups\E.com!"; print "$str\n"; # Add a non-word character to \E with a backslash $str = " \QWelcome to welookups's family"; print "$str\n";

welookups is optimized for learning.© welookups. 2018 - 2020 All Right Reserved and you agree to have read and accepted our term and condition.