YouTip LogoYouTip

Perl Data Types

Perl is a weakly typed language, so variables do not need to be declared with a specific type. The Perl interpreter automatically selects the appropriate type based on the context.

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

Number Type and Description
1 Scalar The scalar is the simplest data type in Perl. Variables of this type can be numbers, strings, or floating-point numbers, with no strict distinction. When using a scalar variable, prefix its name with a $ sign. For example: $myfirst=123; # Number 123 $mysecond="123"; # String "123"
2 Array Array variables start with the @ character, and indexing starts from 0. For example: @arr=(1,2,3)
3 Hash A hash is an unordered collection of key/value pairs. 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

1. Integers

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

In most computers, floating-point registers can store approximately 16 digits; anything longer is truncated. Integers are essentially 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; # Equals decimal 39
$var2 = 0x1f; # Equals decimal 31

2. Floating-Point Numbers

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

Floating-point registers often cannot store floating-point numbers precisely, leading to errors. Pay special attention during calculations 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 produces the following output:

First value: 0
Second value: 0.01

3. Strings

Strings in Perl are represented by a scalar. The definition method is similar to C, but in Perl, strings are not terminated by .

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

However, single quotes can be used to define multi-line text, as shown below:

#!/usr/bin/perl
$var='This is an example using
multi-line string text';
print($var);

Executing the above program produces the following output:

This is an example using
multi-line string text

Common escape characters in Perl are listed 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
nn Creates an octal-formatted number
xnn Creates a hexadecimal-formatted number
cX Control character, where X can be any character
u Forces the next character to uppercase
l Forces the next character to lowercase
U Forces all characters to uppercase
L Forces all characters to lowercase
Q Adds backslashes to non-word characters up to E
E Ends L, U, or Q

Example

Let's take a closer look at the usage of single quotes, double quotes, and escape characters:

Example

#!/usr/bin/perl
$str = " n www.";
print "$strn";
$str = ' nwww.';
print "$strn";
$str = "ututorial";
print "$strn";
$str = "Ututorial";
print "$strn";
$str = "Welcome to UtutorialE.com!";
print "$strn";
$str = "QWelcome to tutorial's family";
print "$strn";

The output of the above example is:

Image 1

← Perl ScalarsPerl Environment β†’