Perl String Escaping Characters

All the special characters or symbols like @, #, $, & /, \, etc does not print in a normal way. They need a preceding escaping character backward slash (\) to get printed.

Displaying E-mail Address

All the e-mail addresses contain (@) sign. As stated earlier, symbols will not be printed normally inside a string. They need extra attention. Use backward slash (\) before @ sign to print e-mail addresses.

Example
snippet
use strict;
use warnings;
my $site  = "rookienerd\@gmail.com";
print "$site\n";
Output
rookienerd@gmail.com

$ sign Embedding in Double quote String

If we want to print ($) sign inside a string, use backward slash (\) preceding $ sign.

Example
snippet
use strict;
use warnings;
my $msg1 = 'Ana';
print "We have defined \$msg1 as $msg1\n";
Output
We have defined $msg1 as Ana

Escaping Escape Character

If we want to print (\) sign inside a string, use backward slash (\) preceding \ sign.

Example
snippet
use strict;  
use warnings;  
print "The \\n is a new line chracter in programming languages.\n";
Output
Everyone has to follow this rule whether its a boy\girl

Escaping Double quotes

If you want to print double quotes inside a string use backslash (\) at both the quotes.

Example
snippet
use strict;
use warnings;
my $x = 'tutorials';
print "Our site \"rookienerd\" provides all type of \"$x\"\n";
Output
Our site "rookienerd" provides all type of "tutorials?

Double-q Operator, qq

The "qq" operator replaces the double quote surrounding a string by its parentheses. It means ("") are not essential on this string anymore. It will simply print the string with qq.

But if you want a bracket inside a string then you need to use curly braces {} surrounding the string.

And if you need curly braces inside the string then use square bracket [] surrounding the string.

Example
snippet
#use of qq
use strict;
use warnings;
my $x = 'tutorials';
print qq(Our site "rookienerd" provides all type of "$x"\n);
#use of {} brackets
print qq{Our site (rookienerd) provides all type of "$x"\n};
#use of [] brackets
print qq[Our site (rookienerd} provides all type of "$x"\n];
Output
Our site "rookienerd" provides all type of "tutorials" Our site (rookienerd) provides all type of "tutorials" Our site (rookienerd} provides all type of "tutorial"

Perl Single-q Operator, q

The single 'q' operator works as the single quote (') in the string. Like single quote, it also does not interpolate the variables.

Example
snippet
#use of q
use strict;
use warnings;
my $x = 'tutorials';
print q(Our site "rookienerd" provides all type of "$x"\n);
print"\n";
#use of () brackets
print q(Our site "rookienerd" provides all type of "$x"\n);
print"\n";
#use of {} brackets
print q{Our site )rookienerd( provides all type of "$x"\n};
print"\n";
#use of {} brackets
print q[Our site )rookienerd} provides all type of "$x"\n];
Output
Our site "rookienerd" provides all type of "$x"\n Our site "rookienerd" provides all type of "$x" \n Our site )rookienerd( provides all type of "$x" \n Our site )rookienerd} provides all type of "$x" \n
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +