Perl Single and Double Quoted String Literals

Here, we are going to learn about the Perl single and double quoted string literals with syntax and examples.
Submitted by Godwill Tetah, on December 11, 2020

In this tutorial, we will learn about the variables holding string value or string literals.

Unlike other languages, double and single quotes have their significance. Let us remind ourselves how variables are written in Perl.

$My_name = "Godwill Tetah"

Now, we ask ourselves, what makes the difference between double and single quote?

Single Quote Strings

In Perl, when a string has single quote, it means everything is treated as a literal mere text. Even if it is the name of a variable.

# Single Quote Strings

$x = 5;
$my_name = 'Include help $x';

print $my_name;


Include help $x

As you can see from the example above, the variable name $x has been displayed just by its name $x.

Now, let us give it a try with double quotes.

Double Quote Strings

Run the above example with double quotes and see the output.

# Double Quote Strings

$x = 5;
$my_name = "Include help $x";

print $my_name;


Include help 5

The difference between single and double-quotes is that double quotes insert variables and special characters such as newlines \n, whereas a single quote does not insert any variable or special character.

$name = "Alex";
$age = 21;

# double quotes
print "Name: $name\nAge: $age\n";

# single quotes
print 'Name: $name\nAge: $age\n';


Name: Alex
Age: 21
Name: $name\nAge: $age\n

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.