Perl Data Types
Perl - Data Types
Section titled “Perl - Data Types”Perl is a dynamically and loosely typed language. This means you don’t need to explicitly declare the type of a variable; Perl infers the type based on the context in which the data is used.
Perl has three fundamental built-in data types:
| S.N. | Type and Description |
|---|---|
| 1 | Scalars |
| 2 | Arrays |
| 3 | Hashes |
Numeric Literals
Section titled “Numeric Literals”Perl handles numbers internally as either signed integers or double-precision floating-point values, converting between them as needed.
Numeric literals can be specified in various formats:
| Type | Example Value(s) |
|---|---|
| Integer | 1234, -100 |
| Floating-point | 3.14159, -0.5, 2.0 (or 2.) |
| Scientific notation | 6.022e23 (for 6.022 x 10^23), 1.0E-9 |
| Hexadecimal (base 16) | 0xFF (255 in decimal), 0xbeef |
| Octal (base 8) | 0755 (493 in decimal), 012 (10 in decimal) (Note: leading zero for octal) |
| Binary (base 2) | 0b1010 (10 in decimal), 0b1111_0000 |
| Underscores for readability | 1_000_000 (one million), 3.141_592_653 (underscores are ignored by Perl) |
String Literals
Section titled “String Literals”Strings are sequences of characters. They can be defined using single quotes ('...') or double quotes ("...").
- Single-quoted strings (
'...'): Perform almost no interpolation. Only\'(to get a literal single quote) and\\(to get a literal backslash) are interpreted. Variable names like$nameare treated literally. my $text = 'This is $var literally.\n'; # $var and \n are not expanded- Double-quoted strings (
"..."): Perform variable interpolation (scalar and array variables are replaced by their values) and escape sequence expansion (e.g.,\nbecomes a newline). my $name = "Perl"; my $greeting = "Hello, $name!\n"; # $name is replaced, \n becomes newline
Common escape sequences usable in double-quoted strings (and similar contexts like qq// or regexes):
| Escape sequence | Meaning |
|---|---|
\n | Newline |
\r | Carriage return |
\t | Horizontal tab |
\f | Form feed |
\b | Backspace (usually) |
\a | Alert (bell) |
\e | Escape character (ASCII ESC, 0x1B) |
\" | Literal double quote (needed inside "...") |
\' | Literal single quote (needed inside '...') |
\\ | Literal backslash |
\$ | Literal dollar sign (to prevent interpolation of $variable) |
\@ | Literal at-sign (to prevent interpolation of @array) |
\0NNN | Character specified by an octal number (NNN) |
\xNN | Character specified by a hexadecimal number (NN) |
\x{NNNN} | Character specified by a hexadecimal number (Unicode code point) |
\N{CHARNAME} | Named Unicode character (e.g., \N{GREEK SMALL LETTER PI}). Requires use charnames ':full'; or similar. |
\u | Uppercase next character. |
\l | Lowercase next character. |
\U | Uppercase all following characters until \E. |
\L | Lowercase all following characters until \E. |
\Q | Quote (backslash) all following non-alphanumeric characters until \E (useful for literal strings in regex). |
\E | End \U, \L, or \Q modification. |
Example of String Literals and Escapes
Section titled “Example of String Literals and Escapes”#!/usr/bin/env perluse strict;use warnings;use utf8; # Allows UTF-8 in the source codeuse charnames ':full'; # For \N{...}
my $language = "Perl";
# Double-quoted string with interpolation and escapesmy $str1 = "Welcome to \t$language!\nThis is fun.";print "String 1:\n$str1\n";
# Single-quoted string (no interpolation)my $str2 = 'The variable $language is not expanded here. \n is literal.';print "String 2:\n$str2\n\n";
# Case modificationmy $str3 = "\uall \Llower THEN \UUPPER\E case again.";print "String 3: $str3\n";
# Quoting for regexmy $search_term = "(some.text)*";my $quoted_term = "\Q$search_term\E";print "String 4: Original term: '$search_term', Quoted for regex: '$quoted_term'\n";
# Unicode charactermy $str5 = "Pi symbol: \N{GREEK SMALL LETTER PI} is \x{03c0}";print "String 5: $str5\n";This will produce the following result:
String 1:Welcome to Perl!This is fun.
String 2:The variable $language is not expanded here. \n is literal.
String 3: All lower then UPPER case again.String 4: Original term: '(some.text)*', Quoted for regex: '\(some\.text\)\*'String 5: Pi symbol: π is πPerl also provides quote-like operators (q//, qq//, qw//, qx//, qr//) for more flexible ways to define strings, lists of words, execute commands, or compile regular expressions, allowing different delimiters.