Quoting and interpolation
Double quotes fill in variables for you, and single quotes keep every character exactly as typed.
In this page:
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
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
Login to try C/C++/Java/PHP code in the editor
Common Mistakes
- Expecting variables to be replaced inside single quotes
- Forgetting to escape @ in a double-quoted email address
- 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: