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.

Perl 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.

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

Output:

Output
javatpoint@gmail.com

Perl $ sign Embedding in Double quote String

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

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

Output:

Output
We have defined $msg1 as Ana

Perl Escaping Escape Character

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

snippet
use strict;  
use warnings;  
print "The \\n is a new line chracter in programming languages.\n";

Output:

Output
Everyone has to follow this rule whether its a boy\girl

Perl Escaping Double quotes

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

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

Output:

Output
Our site "javaTpoint" provides all type of "tutorials?

Perl 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.

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

Output:

Output
Our site "javaTpoint" provides all type of "tutorials" Our site (javaTpoint) provides all type of "tutorials" Our site (javaTpoint} 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.

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

Output:

Output
Our site "javaTpoint" provides all type of "$x"\n Our site "javaTpoint" provides all type of "$x" \n Our site )javaTpoint( provides all type of "$x" \n Our site )javaTpoint} 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