Perl Part I · Module 4 · Strings and Text
Single Quotes vs Double Quotes
Perl has two ways to write a string literal and they are not a matter of house style. Double quotes hand the text to Perl for processing — variables are replaced by their values and backslash sequences become control characters. Single quotes keep almost everything exactly as typed. The difference is easiest to believe as a number: write a Windows-style path twelve characters long and the single-quoted version is twelve characters while the double-quoted version is ten, because a backslash-n inside it quietly became a newline. This chapter establishes which form to reach for, and corrects the common half-truth that single quotes have no escapes at all — they have exactly two, and not knowing the second one produces a compile error with a confusing message.
Foundation13 min readPerlPart IFundamentalsStringsQuoting
Part I · Module 4 · Chapter 4.1 · Single Quotes vs Double Quotes
1. Two Kinds of String Literal
use strict;
use warnings;
my $name = "Ravi";
print "double: $name\n";
print 'single: $name', "\n";double: Ravi
single: $nameTwo lines that differ only in the quote character, and they print different things.
- Double quotes are processed. Perl looks through the text, replaces variables with their values, and turns backslash sequences into the characters they name.
- Single quotes are literal. What you typed is what you get, with two small exceptions that §4 covers.
Note the second line still needed "\n" in double quotes to end the line — inside single quotes, \n would have printed as a backslash followed by an n.
Chapter 2.5 introduced interpolation in passing; this module takes it seriously. The replacement of variables is Chapter 4.2's subject and the backslash sequences are Chapter 4.3's. What this chapter settles is which form to reach for.
2. The Demonstration That Settles It
A Windows-style path is the classic case, because it is full of backslashes:
use strict;
use warnings;
my $single = 'C:\new\table';
my $double = "C:\new\table";
printf "single: [%s] len=%d\n", $single, length($single);
printf "double: [%s] len=%d\n", $double, length($double);single: [C:\new\table] len=12
double: [C:
ew able] len=10Read the lengths. You typed the same twelve characters both times. The single-quoted string is 12 characters. The double-quoted string is 10.
Here is what happened to the four missing characters. Inside double quotes:
\nis not a backslash and ann. It is one character — a newline — which is why the output breaks across two lines.\tis not a backslash and at. It is one character — a tab — which is the gap beforeable.
So C:\new\table became C:, a newline, ew, a tab, able. Twelve typed characters, ten stored, and the string is now unusable as a path.
3. The Fork, Drawn
One typed text, two quoting rules, two different strings
The two results are different strings, not two renderings of one string. Nothing downstream can tell that the lower path lost information, because it received a perfectly valid ten-character string.
4. Single Quotes Have Exactly Two Escapes
It is often said that single quotes treat every character literally. That is close, and the gap matters.
use strict;
use warnings;
my $one = 'a\b';
my $two = 'a\\b';
printf "'a\\b' -> [%s] len=%d\n", $one, length($one);
printf "'a\\\\b' -> [%s] len=%d\n", $two, length($two);'a\b' -> [a\b] len=3
'a\\b' -> [a\b] len=3Both are three characters, and both contain one backslash. So:
\bis literal — a backslash and ab, two characters. Inside double quotes\bwould be a backspace.\\is an escape even in single quotes, producing one backslash.
The second escape is \', which is how you put a single quote inside a single-quoted string: 'it\'s'.
Why that matters: the trailing backslash
Because \\ is an escape, a lone backslash at the end of a single-quoted string escapes the closing quote:
my $x = 'abc\';Can't find string terminator "'" anywhere before EOF at trailing.pl line 1.255That message names the end of the file rather than line 1's real problem, which makes it one of the more confusing diagnostics in Perl. The cause is simply that \' was read as an escaped quote, so Perl kept looking for the string's real ending and ran out of file.
A directory path ending in a separator hits this: 'C:\logs\' does not compile. Write 'C:\logs\\' or drop the trailing separator.
5. Choosing Between Them
The decision is about the content, not about length or habit:
| The text contains | Use | Because |
|---|---|---|
| a variable you want replaced | "..." | only double quotes interpolate |
\n, \t or another control character | "..." | only double quotes convert them |
a literal $ or @ | '...' | no escaping needed |
| backslashes meant literally — paths, patterns | '...' | §2 is what happens otherwise |
| nothing special at all | either | pick one and be consistent |
For that last row, many Perl codebases prefer single quotes for plain text, on the grounds that a reader can then assume any double-quoted string has something in it worth looking at. That is a reasonable convention and this track follows it loosely, but it is a convention and not a rule.
use strict;
use warnings;
my $block = "alu";
my $message = "running $block regression\n";
my $pattern = '^\s+ERROR';
my $literal = 'cost is $5 per run';
print $message;
print "pattern: $pattern\n";
print "literal: $literal\n";running alu regression
pattern: ^\s+ERROR
literal: cost is $5 per runThree strings, three different reasons. $message needs the variable and the newline. $pattern needs its backslash kept intact — it is destined for a regular expression in Module 11, and in double quotes \s would be fine but the habit of single-quoting patterns avoids ever having to check. $literal contains a dollar sign that is money, not a variable.
6. Why This Matters in Engineering Work
File paths and glob patterns. Script work in this trade is full of paths, and on a Windows or mixed-site flow they carry backslashes. '\\server\proj\netlist' is a path; "\\server\proj\netlist" contains a backslash, a newline-free s, and whatever \p and \n turn into. The failure is not a crash — it is a file-not-found on a path that looks right in the error message.
Regular expression text. A pattern is mostly backslashes: \s, \d, \., \(. Keeping patterns in single quotes means the pattern Perl compiles is the pattern you typed. Module 11 depends on this habit.
Currency, costs and shell text. '$5', 'user@host', '${HOME}' — anything containing a sigil that is not a variable. In double quotes each of these is either interpolated or a compile error under use strict, which Chapter 4.2 demonstrates.
Messages that a human reads. These genuinely want double quotes, because they contain values and line endings. That is the case double quotes exist for, and most of the strings in a report are this kind.
7. Common Mistakes
Putting a literal path in double quotes. \n, \t, \a, \b, \e and \0 all become control characters. §2 measures the damage.
Believing single quotes have no escapes. They have two: \\ and \'.
Ending a single-quoted string with a backslash. 'abc\' does not compile, and the error names the end of the file.
Expecting '...\n' to end a line. It prints a backslash and an n. Use "\n".
Using double quotes for a string containing $ or @. Either it interpolates something you did not mean, or use strict refuses to compile it.
Assuming the corruption will be obvious. A string with a newline in the middle prints as two lines, which reads as a formatting glitch rather than as data loss.
8. Exercise
Exercise 1 — measure the difference. Run the §2 script and record both lengths. Then write one sentence naming the two typed characters that became one character each, and say which single character each became.
Exercise 2 — see it with your own eyes. Print the double-quoted path again, this time piping the output through cat -v, which shows control characters visibly. Record what you see and match each marker to the escape that produced it.
Exercise 3 — the two single-quote escapes. Predict the length of each before running: 'a\b', 'a\\b', 'it\'s', '100%'. Then measure all four with length() and explain any you got wrong.
Exercise 4 — reproduce the confusing error. Write a one-line script containing my $dir = 'C:\logs\'; and run perl -c on it. Record the exact message and the exit status, then give two different one-character-or-more fixes.
Exercise 5 — choose for each case. For each, say which quote form you would use and why in one clause: (a) a message reporting how many tests failed; (b) a regular expression matching a leading tab; (c) a note that a licence costs $200; (d) a Linux path /proj/alu/syn; (e) a Windows path D:\runs\nightly; (f) a line that must end with a newline.
Exercise 6 — repair a corrupted path. You are handed the line my $out = "D:\results\nightly\today.log";. Say how many characters the string actually contains and which escapes fired, then rewrite the line so the path is intact — twice, once with single quotes and once keeping double quotes.
9. Summary
- Double quotes process the text: variables are replaced and backslash sequences become control characters.
- Single quotes keep it literal, with exactly two escapes:
\\and\'. - The difference is measurable.
'C:\new\table'is 12 characters;"C:\new\table"is 10, because\nand\teach became one control character. - Nothing warns, and a string with an embedded newline prints as a formatting oddity rather than as corruption.
- A trailing backslash breaks a single-quoted string, and the error names the end of the file rather than the line.
- Choose by content: variables or control characters want double quotes; literal backslashes,
$and@want single quotes.
Chapter 4.2 takes the half of this that double quotes exist for — putting a variable's value into the text — and covers the one piece of syntax that makes it reliable when a name runs straight into the words after it.
Where this fits
Part of the Perl curriculum.
