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.
In Perl there are two different types of scalar constants:
- Numeric literal
- 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:
||A large 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
||Denotes carriage return
||Denotes horizontal tab
||Denotes vertical tab
||Backslash following all non-alphanumeric character
||Denotes form feed
||Change next character to uppercase
||change all following characters to uppercase
||Change next character to lowercase
||Change all following character to lowercase
||Denotes \U, \L, \Q
||Controls characters, X is a variable
||Create octal formatted numbers
||Create hexadecimal formatted numbers
||Denote single quote
||Denote double quote