What is the difference between single and double quotes in Perl

In the Perl programming language to create a string in your program code you can use single ' or double quotes ".

There's a difference. In single quotes work only escape sequences \\ and \', in double quotes, except these two work some other escape sequence. And only if you use double quotes, variable names unfolding in value (interpolation occurs). Further details.


Single and double quotes in the Perl programming language are used to describe strings in program code (the lines in the program text are called "string literals"). Here's an example:

▶ Run

my $str1 = 'Hello, world!';
my $str2 = "Hello, world!";

In this program, we created two variables, and each of them has appropriated the line. In this example, there is no difference between using double and single quotes, the string we received in the first and in the second case, absolutely same.


Another common thing is to use double and single quotes is how to act if you need to in the line was a symbol backslash \. If you want to create a line with this symbol, in a string literal you need to write two backslash \\. In the resulting string will be a single backslash. Here's an example:

▶ Run

my $str1 = '\\';
my $str2 = "\\";

print $str1, "\n";
print $str2, "\n";

This program will display:


The quotes in the string

The first difference between single and double quotes is that you need to do to create a line which have single or double quotes.

Using single quotes it is very easy to create a string which has double quotes: '"'. But if need to make a string which has single quote, it must be written using two characters: reverse slash \ and quote. '\'' — such a string literal creates a string from one symbol "single quote".

In the case of double quotes — all the way around. It is convenient to create a string with a single quote "'", and to to place in the string, the double quote need to write before it with a backslash (this is called "screening") "\"".

The use of special characters

When you use single quotes there are only two sets of symbols that are transformed into a single character. This, as we have seen earlier, two back slashes \\ — they turn into a single backslash; and backslash, and single quote \' is transformed into one symbol "single quote".

But when you use double quotes there are several such set of symbols (usually referred to as the "escape sequence"). The most famous and frequently used is a combination of symbols \n. \nin double quotes turns into one special character "newline". If single quotes print 'ASDF\nQWERTY'; displays ASDF\nQWERTY, and if you use double quotes print "ASDF\nQWERTY"; will appear on the screen two lines:


Other used escape sequence is \t, when used in double quotes it becomes in the tab character. Not very often, but still used \b, this special character erases the previous character (can be used to create very simple animations in the console). There are some more escape sequences, they are little used.

Interpolation of variables

If the string created using double quotes to write the variable name, the string name will appear the value of this variable (when using single quotes in the string will be the name).

This feature of language is very often used.

▶ Run

my $var = 'John';

my $str1 = 'Hello, $var';
my $str2 = "Hello, $var";

print $str1, "\n";
print $str2, "\n";

This program will display:

Hello, $var
Hello, John

String $str1 we created using single quotes in the string contains exactly what was written in the string literal — 'Hello, $var'. And in line $str2 variable name $var replaced by the value of this variable.

Please note that only deploys scalars and arrays:

▶ Run

my $var = 'John';
my @arr = ('one', 'two');
my %h = (
    a => 1,

sub my_sub {
    return 12;

print "$var @arr %h &my_sub";

This program displays the text John one two %h &my_sub. The scalar $var and the array @arr turned in value and hash %h and Saba my_sub — no.


In many cases, to create string literals anyway, so and single and double quotes will work the same. Recommendation is this — if a string Litera still use it, use single quotation marks.

Other articles