TExcelTAD
Home › Perl › Perl Strings

Perl Strings

Strings are where Perl shines. A string is a sequence of characters of any length, from the empty string "" to a whole file read into memory.

Single vs double quotes

The two kinds of quotes behave very differently:

Quotes Interpolates variables? Escapes like \n, \t Example
'single' no only \' and \\ 'Cost: $5\n' is printed literally
"double" yes yes "Hi $name\n" inserts $name and a newline
perl · try it yourself
use strict;
use warnings;

my $name = "Ada";
print 'Hello, $name\n', "\n";
print "Hello, $name\n";
print "Tab:\tafter tab\n";
print "Quote: \"Perl\" and a backslash: \\\n";
Hello, $name\n
Hello, Ada
Tab:	after tab
Quote: "Perl" and a backslash: \

Tip: Use single quotes when the string contains no variables. It signals to readers that nothing special happens inside.

Interpolating arrays and expressions

Arrays interpolate with their elements separated by spaces. When a variable name runs into following text, wrap the name in braces:

perl · try it yourself
use strict;
use warnings;

my @fruits = ("apple", "pear", "plum");
my $count  = 3;
my $unit   = "kilo";

print "Fruits: @fruits\n";
print "First: $fruits[0], last: $fruits[-1]\n";
print "We bought $count ${unit}s\n";
print "Total items: @{[ $count * 2 ]}\n";
Fruits: apple pear plum
First: apple, last: plum
We bought 3 kilos
Total items: 6

${unit}s keeps Perl from looking for a variable called $units. The odd-looking @{[ ... ]} trick evaluates an expression inside a string, but most people simply concatenate instead.

Concatenation and repetition

Operator Meaning Example Result
. concatenate "ab" . "cd" "abcd"
x repeat "-" x 5 "-----"
.= append $s .= "!" adds ! to $s
perl · try it yourself
use strict;
use warnings;

my $title = "Monthly Report";
my $line  = "=" x length($title);

print "$title\n$line\n";

my $log = "start";
$log .= " -> load";
$log .= " -> save";
print $log . "\n";
Monthly Report
==============
start -> load -> save

Custom quote operators

When a string is full of quotes, escaping them gets ugly. q() acts like single quotes and qq() like double quotes, with any delimiter you choose:

perl · try it yourself
use strict;
use warnings;

my $user = "maria";
my $single = q(It's "quoted" without escapes);
my $double = qq{User "$user" logged in};
my $path   = q[C:\Users\maria];

print "$single\n$double\n$path\n";
It's "quoted" without escapes
User "maria" logged in
C:\Users\maria

Bracket-like delimiters ((), {}, [], <>) nest properly, so q(a (b) c) works as expected.

String comparison

Strings have their own comparison operators made of letters: eq, ne, lt, gt, le, ge and cmp. Using == on strings is a classic bug, because == compares them as numbers:

perl · try it yourself
use strict;
use warnings;
no warnings 'numeric';   # silence the warning to show the bug clearly

my $answer = "yes";
print "eq says: ", ($answer eq "no" ? "same" : "different"), "\n";
print "== says: ", ($answer == "no" ? "same" : "different"), "\n";
print "cmp: ", "apple" cmp "banana", "\n";
eq says: different
== says: same
cmp: -1

Both "yes" and "no" are 0 as numbers, so == wrongly claims they are equal. The next lesson covers all the operators side by side.

Test yourself

What does print 'Total: $sum'; print?

  1. The value of $sum
  2. Total: $sum literally
  3. An error
  4. Total: followed by nothing

What is "ab" x 3?

  1. "ab3"
  2. "ababab"
  3. an error
  4. "aabbcc"

Which operator correctly tests whether two strings are equal?

  1. ==
  2. =
  3. eq
  4. ===