Scalars, Numbers and Strings

3.1 Scalars

A scalar holds a single value: a number, a string, a reference, or undef.

Perl converts automatically: "10" + 5 is 15; 10 . "5" is "105". With warnings on, "abc" + 1 warns ("Argument isn't numeric").

use v5.36;
my $age     = 30;              # integer
my $price   = 19.99;           # floating point
my $big     = 1_000_000;       # underscores for readability
my $hex     = 0xFF;            # 255
my $oct     = 0o755;           # 493 (0o prefix in 5.34+; 0755 also works)
my $bin     = 0b1010;          # 10
my $sci     = 1.5e3;           # 1500
my $name    = "Alice";         # string
my $undef;                     # undef (no value)
my ($x, $y) = (10, 20);        # list assignment

3.2 Strings and Quoting

my $lang = 'Perl';
my $s1 = 'Single: no $interpolation, only \\ and \' escapes';
my $s2 = "Double: $lang, tab\there, newline\n";
my $s3 = "Array: @{[ 1 + 2 ]} and expression interpolation";
my $s4 = "Method call: @{[ scalar localtime ]}";
my $s5 = "${lang}5";                   # braces disambiguate the variable name

# Quote-like operators: choose any delimiter
my $q   = q{It's single-quoted};        # like '...'
my $qq  = qq{He said "hi" to $lang};    # like "..."
my @w   = qw(apple banana cherry);      # list of words
my $cmd = qx(ls -l);                    # backticks: command output

# Heredocs
my $text = <<"END";
Hello, $lang!
  Interpolated heredoc.
END

my $raw = <<'END';
No $interpolation here.
END

my $indented = <<~EOT;                  # indented heredoc (5.26+): strips leading indentation
    Line one
    Line two
    EOT

3.3 Common String Escapes

\n, \t, \r Newline, tab, carriage return \x{263A} / \N{U+263A} Unicode code point \N{GREEK SMALL LETTER ALPHA} Character by name Uppercase/lowercase the next character \U... \E / \L... \E Uppercase/lowercase until \E Quote (escape) regex metacharacters

3.4 String Functions

my $s = "  Hello, Perl World  ";
length $s;                     # 22
uc $s; lc $s; ucfirst 'perl'; lcfirst 'PERL'; fc 'Straße';   # fc = fold case (5.16+)
index($s, 'Perl');             # position or -1
rindex($s, 'l');
substr($s, 2, 5);              # "Hello"
substr($s, 2, 5) = 'Howdy';    # substr as an lvalue (replaces in place)
my $t = $s =~ s/^\s+|\s+$//gr; # trim (returns a new string with /r)
use builtin qw(trim);          # builtin::trim (stable in 5.40; experimental in 5.36–5.38)
my $trimmed = trim($s);
reverse 'abc';                 # in scalar context: "cba"
my $rev = scalar reverse 'abc';
'ab' x 3;                      # "ababab" (repetition)
join '-', 'a', 'b', 'c';       # "a-b-c"
split /,\s*/, 'a, b,c';        # ('a', 'b', 'c')
sprintf '%05.2f', 3.14159;     # "03.14"
ord 'A'; chr 65;               # 65, "A"
lc($a) eq lc($b);              # case-insensitive comparison
my $line = "text\n"; chomp $line;   # remove a trailing newline (returns the count removed)
chop $line;                    # remove the last character (rarely needed)

3.5 Numbers and Math

say 7 / 2;          # 3.5 (Perl numbers are floats when needed)
say int(7 / 2);     # 3
say 7 % 3;          # 1
say -7 % 3;         # 2 (sign follows the right operand)
say 2 ** 10;        # 1024
say abs(-5), sqrt(16), exp(1), log(exp(1));
say sin(0), cos(0), atan2(1, 1) * 4;          # pi
say int(rand(100));                            # random integer 0..99
srand(42);                                     # seed the random generator
say sprintf('%.2f', 2.675);                    # rounding via sprintf
use POSIX qw(floor ceil fmod);
say floor(-2.5), ceil(2.1);
use List::Util qw(sum max min sum0 product);
say sum(1..10), max(3, 9, 2);
use bigint;  say 2 ** 100;                     # arbitrary precision (lexically scoped)
use Math::BigInt; my $n = Math::BigInt->new('123456789012345678901234567890');

3.6 undef and Truthiness

False values: undef, 0, 0.0, '' (empty string), '0'. Everything else is true, including '0.0', '00', and ' '.

my $v;
say defined $v ? 'defined' : 'undef';
my $port = $ENV{PORT} // 8080;     # defined-or (5.10+): uses 8080 only if undef
my $name = $input || 'default';    # logical or: uses the default for any false value
$count //= 0;                      # assign if undef
use builtin qw(true false is_bool);   # real booleans (5.36+, stable in 5.40)