← Back to Perl Course | Chapter 3: Strings | Lesson 1 of 7

Quoting and interpolation

Double quotes fill in variables for you, and single quotes keep every character exactly as typed.

Quoting and interpolation

Inside double quotes, Perl replaces variables with their values and understands escapes such as \n and \t. Single quotes do neither, except that \' and \\ still work. The q() and qq() operators are alternatives to single and double quotes that let you pick your own delimiters, which is handy when the text itself contains quotes.

Note: Use ${name} or @{[ expression ]} when interpolation needs clear boundaries or a calculation.

Example: Quoting and interpolation

perl
use strict;
use warnings;

my $name = "Perl";
my @langs = ("Perl", "C");
print "Hello, $name!\n";
print 'Hello, $name!\n', "\n";
print "Suffix: ${name}_script\n";
print "Array: @langs\n";
print "Email: user\@example.com\n";
print "Sum: @{[ 2 + 3 ]}\n";
print qq(He said "hi" to $name\n);
print q(It's literal: $name), "\n";

# Output:
# Hello, Perl!
# Hello, $name!\n
# Suffix: Perl_script
# Array: Perl C
# Email: [email protected]
# Sum: 5
# He said "hi" to Perl
# It's literal: $name
Common Mistakes
  1. Expecting variables to be replaced inside single quotes
  2. Forgetting to escape @ in a double-quoted email address
  3. Writing $name_text when you meant ${name}_text
Chapter Summary
  • Double quotes interpolate variables and escapes
  • Single quotes are literal
  • q() and qq() choose custom delimiters
  • Use ${name} to mark the end of a variable name
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.