Skip to content
VLSI Mentor

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

Azvya Education Pvt. Ltd.VLSI Mentor
two-forms.pl
use strict;
use warnings;

my $name = "Ravi";

print "double: $name\n";
print 'single: $name', "\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
double: Ravi
single: $name

Two 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:

Azvya Education Pvt. Ltd.VLSI Mentor
path.pl
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);
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
single: [C:\new\table] len=12
double: [C:
ew	able] len=10

Read 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:

  • \n is not a backslash and an n. It is one character — a newline — which is why the output breaks across two lines.
  • \t is not a backslash and a t. It is one character — a tab — which is the gap before able.

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

A fork with five blocks. At the middle left is the text as typed into the source file, the twelve characters C colon backslash n e w backslash t a b l e. Two paths lead from it. The upper path is the single-quoted form: Perl keeps every character as typed, and the result is a twelve-character string that is still a usable path. The lower path is the double-quoted form: Perl processes backslash sequences, so backslash-n becomes one newline character and backslash-t becomes one tab character, and the result is a ten-character string containing two control characters, which is no longer a usable path. The same input therefore yields two strings of different lengths depending only on the quote character chosen.As typed in sourceC:\new\tableSingle quotes keepno backslash processing12 charactersstill a usable pathDouble quotes process\n and \t convert10 charactersnewline + tab insidesingle quotesdouble quotes12
Both paths are correct behaviour for the form that was written — neither is a bug in Perl. The difference is decided entirely by which quote character the author typed, and the only cheap evidence that the right one was chosen is the length.

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.

Azvya Education Pvt. Ltd.VLSI Mentor
single-escapes.pl
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);
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
'a\b'  -> [a\b] len=3
'a\\b' -> [a\b] len=3

Both are three characters, and both contain one backslash. So:

  • \b is literal — a backslash and a b, two characters. Inside double quotes \b would 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:

Azvya Education Pvt. Ltd.VLSI Mentor
trailing.pl
my $x = 'abc\';
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output — STDERR
Can't find string terminator "'" anywhere before EOF at trailing.pl line 1.
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output — perl -c exit status
255

That 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 containsUseBecause
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 alleitherpick 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.

Azvya Education Pvt. Ltd.VLSI Mentor
choosing.pl
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";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
running alu regression
pattern: ^\s+ERROR
literal: cost is $5 per run

Three 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 \n and \t each 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.