BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Perl String Escape Sequences

Last Updated: March 30, 2018 by Chaitanya Singh | Filed Under: Perl

In the previous tutorial we have learned how to work with Strings in Perl. In this guide, we will discuss the escape characters that will help us achieve desired output in certain cases.

Displaying email address in Perl

The character @ has a special meaning in perl. As we already know that when we place the special characters inside double quote strings then perl tries to interpolate it. In the following example if we do not place the backslash before the @ then instead of displaying the email, it would throw an error because it will consider @gmail as an array.

In case of single quotes no need to use the the escape sequence because interpolation doesn’t happen in single quote strings.

use strict;  
use warnings;  

my $email  = "xyz\@gmail.com";  
print "$email\n";  

# no backslash needed as interpolation does not
# work in single quotes.
my $email2  = '[email protected]';  
print "$email2\n";

Output:

[email protected]
[email protected]

Escaping the $ sign in double quote strings

As we already know that dollar sign interpolates to the value of the variable. In case you want to escape the $ sign and avoid the interpolation, use the same trick we have done above – prefix it with backslash.

use strict;
use warnings;
my $name = 'Negan';
my $msg = 'I am Negan';

# escaping the first dollar sign but not escaping the second
print "\$name: $name\n";

# escaping the first dollar sign but not escaping the second
print "\$msg: $msg\n";

Output:

$name: Negan
$msg: I am Negan

How to escape the escape character backslash (\)

In the above examples, we have used the backslash to escape the special characters $ and @. There may be a case where you would want to display the backslash in the output. For this, you would want to escape the backslash. Lets take an example to understand this:

use strict;
use warnings;
my $say = 'I do like to use backslash \\';
print "$say\n";

Output:

I do like to use backslash \

As you can see we have \ displayed in the output.

Escaping the double quotes in the string

We know that a text inside double quotes is treated as string in Perl. Lets say we want to display a String in Perl and the string itself has a double quotes in it. We will use the same approach by escaping the quotes using \.

use strict;
use warnings;
my $say = "I like to watch \"The Walking Dead\"";
print "$say\n";

Output:

I like to watch "The Walking Dead"

Double q operator – qq

We can replace the double quotes that we use to enclose a string with the double q operator. The advantage of doing this is that we need not to worry about using escape sequences for double quotes(“) and brackets.

use strict;
use warnings;
my $name = 'Chaitanya';
print qq(My name is "$name" and I like brackets ()\n);

Output:

My name is "Chaitanya" and I like brackets ()

See I have not used the escape sequences for the double quotes and brackets.

Single q operator – q

Single q operator works like single quotes. The special characters present inside it does not interpolate.

Lets take the same example that we have seen above in double q operator.

use strict;
use warnings;
my $name = 'Chaitanya';
print q(My name is "$name" and I like brackets ()\n);

Output:

My name is "$name" and I like brackets ()\n
❮ Previous

Top Related Articles:

  1. Subroutines in Perl
  2. First Perl Program
  3. my keyword – Local and global variables in Perl
  4. Perl Variables
  5. Perl Tutorial for beginners

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Perl Tutorial

  • Perl Tutorial
  • Perl Installation
  • First Perl Program
  • Perl Syntax
  • Data types in Perl
  • Perl Variables
  • my keyword
  • Perl Scalars
  • Use strict and use warnings
  • Perl Arrays
  • Perl Hashes
  • Operators in Perl
  • Perl Conditional statements
  • Perl if
  • Perl if-else
  • Perl if-elsif-else
  • Perl unless
  • Perl unless-else
  • Perl unless-elsif-else
  • Perl switch case
  • Perl given-when-default
  • Perl loops
  • Perl subroutines
  • Perl Strings
  • Perl Escape Sequences

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap