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
Leave a Reply