Skip to content

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
1Scalars
2Arrays
3Hashes

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:

TypeExample Value(s)
Integer1234, -100
Floating-point3.14159, -0.5, 2.0 (or 2.)
Scientific notation6.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 readability1_000_000 (one million), 3.141_592_653 (underscores are ignored by Perl)

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 $name are 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., \n becomes 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 sequenceMeaning
\nNewline
\rCarriage return
\tHorizontal tab
\fForm feed
\bBackspace (usually)
\aAlert (bell)
\eEscape 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)
\0NNNCharacter specified by an octal number (NNN)
\xNNCharacter 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.
\uUppercase next character.
\lLowercase next character.
\UUppercase all following characters until \E.
\LLowercase all following characters until \E.
\QQuote (backslash) all following non-alphanumeric characters until \E (useful for literal strings in regex).
\EEnd \U, \L, or \Q modification.
#!/usr/bin/env perl
use strict;
use warnings;
use utf8; # Allows UTF-8 in the source code
use charnames ':full'; # For \N{...}
my $language = "Perl";
# Double-quoted string with interpolation and escapes
my $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 modification
my $str3 = "\uall \Llower THEN \UUPPER\E case again.";
print "String 3: $str3\n";
# Quoting for regex
my $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 character
my $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.