Perl Data Types

Perl language is a loosely typed language, the Perl interpreter itself chooses the data type. Hence, there is no need to specify data type in Perl programming.

There are basically three data types in Perl:

  • Scalars: Perl scalars are a single data item. They are simple variables, preceded by a ($) sign. A scalar can be a number, a reference (address of a variable) or a string.
  • Arrays: Perl arrays are an ordered list of scalars. They are preceded by (@) sign and accessed by their index number which starts with 0.
  • Hashes: Perl hashes are an unordered collection of key-value pairs. They are preceded by (%) sign and accessed using keys.

Perl Literals

In Perl there are two different types of scalar constants:

  1. Numeric literal
  2. String literal

Perl Numeric Literals

Perl numeric literals are numbers. Perl stores number internally as either signed integers or floating-point values.

Perl numeric literals can be assigned following types of formats:

Number Type
526 Integer
5.5 Floating point
5e10 Scientific notation
5.5E Scientific notation
5_549_63 A large number
010101 Binary number
0175 Octal number
AF0230 Hexadecimal number

Look at the above table,

  • Integers are a group of consecutive digits.
  • Floating-point numbers contain a decimal in between. A number containing '0' value on the right hand side of the number (234.00) is also a floating point number .
  • A number containing an exponent notation (e or E) is the scientific notation.
  • Commas are not allowed into a numeric literal but you can use underscores (_) instead of commas. Perl will remove underscores while using this value.
  • Combination of 0 and 1 represents a binary number with base 2.
  • Number with a leading 0 comes in the category of octal numbers with base 8.
  • Number containing alphabets (a, b, c, d, e, f) are hexadecimal numbers with base 16.

Perl String Literals

Perl string literals contain an empty string, ASCII text, ASCII with high bits or binary data. There is no limit in a string to contain data. They are surrounded by either a single quote (′) or double quote (″).

Variable interpolation is allowed in double quote string but not in single quote string. Also special characters preceding with backslash (\) are supported by double quote strings only.

Escape Characters in string literals

Characters Purpose
\n Denotes newline
\r Denotes carriage return
\ t Denotes horizontal tab
\v Denotes vertical tab
\Q Backslash following all non-alphanumeric character
\a Denotes alert
\f Denotes form feed
\b Denotes backspace
\u Change next character to uppercase
\U change all following characters to uppercase
\l Change next character to lowercase
\L Change all following character to lowercase
\E Denotes \U, \L, \Q
\cX Controls characters, X is a variable
\0nn Create octal formatted numbers
\xnn Create hexadecimal formatted numbers
\\ Denote backslash
\' Denote single quote
\" Denote double quote
Related Tutorial
Follow Us #