ADVERTISEMENT
ADVERTISEMENT

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;

Output:

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;

Output:

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';

Output:

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



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.