Easy Tutorial
❮ Perl Environment Perl Sending Email ❯

Perl Data Types

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

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

Number Type and Description
1 Scalar Scalars are the simplest data type in Perl. Variables of this type can be numbers, strings, or floating-point numbers, without strict distinctions. When using them, prefix the variable name with a $, indicating it is a scalar. For example: $myfirst=123; # Number 123 <br>$mysecond="123"; # String "123"
2 Array Array variables start with the @ character, and indexing begins at 0, such as: @arr=(1,2,3)
3 Hash A hash is a collection of key/value pairs that are unordered. You can use the key as an index to retrieve the value. Hash variables start with the % character. %h=('a'=>1,'b'=>2);

Numeric Literals

I. Integer

Perl actually stores integers in your computer's floating-point registers, so they are treated as floating-point numbers.

On most computers, floating-point registers can store about 16 digits, and longer numbers are truncated. Integers are 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 numbers start with 0, and hexadecimal numbers start with 0x. For example:

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

II. Floating-Point Numbers

Floating-point numbers 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, which need special attention during operations and comparisons. The exponent range is typically from -309 to +308.

Example

#!/usr/bin/perl 

$value = 9.01e+21 + 0.01 - 9.01e+21;
print ("First value: ", $value, "\n");
$value = 9.01e+21 - 9.01e+21 + 0.01;
print ("Second value: ", $value, "\n");

Executing the above program outputs:

First value: 0
Second value: 0.01

III. Strings

Strings in Perl are represented using scalars. The definition is similar to C, but strings in Perl are not terminated with \0.

The difference between double quotes and single quotes in Perl: Double quotes can parse escape characters and variables normally, while single quotes cannot and will output them literally.

However, strings defined with single quotes can span multiple lines, as shown below:

#!/usr/bin/perl 

$var='This is an example of using

multi-line string text

in Perl';

print($var);

Executing the above program outputs:

This is an example of using

multi-line string text

in Perl

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

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

Example

Let's look at specific examples of using single quotes, double quotes, and escape characters:

#!/usr/bin/perl

# Newline \n within double quotes is effective
$str = "tutorialpro.org  \nwww.tutorialpro.org";
print "$str\n";

# Newline \n within single quotes is ineffective
$str = 'tutorialpro.org  \nwww.tutorialpro.org';
print "$str\n";

# Only the letter R will be converted to uppercase
$str = "\ututorialpro";
print "$str\n";

# All letters will be converted to uppercase
$str = "\Ututorialpro";
print "$str\n";

# Only part of the string will be converted to uppercase
$str = "Welcome to \Ututorialpro\E.com!"; 
print "$str\n";

# Add backslashes to non-word characters until \E
$str = "\QWelcome to tutorialpro's family";
print "$str\n";

Executing the above program outputs:

❮ Perl Environment Perl Sending Email ❯