AnyCompiler

Strings

Perl has two string literal forms, and they behave differently on purpose. Double-quoted strings interpolate — a $scalar or @array written inside one is replaced with its value. Single-quoted strings are literal — nothing inside them is ever expanded, which makes them the right choice whenever a string should contain a literal $ or @.

use strict;
use warnings;

my $name = "World";
print "Double-quoted: Hello, $name!\n";
print 'Single-quoted: Hello, $name!', "\n";
Output
Double-quoted: Hello, World!
Single-quoted: Hello, $name!

Strings concatenate with the . operator (not +, which is reserved for numeric addition). length, substr, uc, and lc are ordinary built-in functions, not methods — Perl's strings aren't objects you call methods on.

use strict;
use warnings;

my $first = "Perl";
my $second = "Rules";
my $combined = $first . " " . $second;
print "$combined\n";
print length($combined), "\n";
print substr($combined, 0, 4), "\n";
print uc($combined), "\n";
print lc($combined), "\n";
Output
Perl Rules
10
Perl
PERL RULES
perl rules

Next: numbers and operators — and the genuinely distinctive fact that Perl has two completely separate sets of comparison operators, one numeric and one for strings.

Practice exercise

Create my $word = "perl". Print its uppercase version using uc($word), then print its length using length($word).