String , qq , q and Escape character

String

String is a collection of characters and these are written within (' ') or (" "). Strings can be placed either between single quotes ' or double quotes " and they have slightly different behavior.

Single quoted strings

If you put characters between single quotes ', then almost all the characters, except the single-quote itself ', are interpreted as they are written in the code. That means, nothing is interpolated and each character is writen as it is.

#!/usr/bin/perl

use strict;
use warnings;

my $name = 'w3clan';
print 'Hello $name, how are you?\n';

Output :

Hello $name, how are you?\n

Notice couple of thing :-

  1. $name is still $name and it is not replaced with w3clan
  2. \n is still \n and is not replaced or changed to new line.

That is Single quoted String

Double quoted strings

Srings placed between double quotes " provide interpolation (i.e,variables embedded in the string will be replaced by their content) and they also replace the special escape sequences such as \n by a real newline and \t by a real tab.

#!/usr/bin/perl

use strict;
use warnings;

my $name = 'w3clan';
print 'Hello $name, how are you?\n';

Output :-

Hello w3can, How are you?

So, from above you can see, double quote interpolates all special characters known to perl. That means little problem there , see :-

  1. What if you need to store an email address that has "@" in it. "@" has special meaning in perl.
  2. What if you need to store data with dollar sign in it "$" ,  we all know "$" has special meaning in perl.
  3. What if you need to use single quote inside double quote or double quote inside double quote.

Can you imagine, those issues with using single quote and double quote ? Well, if yes, Either you are well aware of other programming language or you are some real intelligent bitch . Now, let's solve the riddle.

Perl provides amazing option to escape such special meaning characters. and it is "\" . 

Escape Characters 

Scenario 1 : Escaping special character.
#!/usr/bin/perl

use strict;
use warnings;

my $name = 'w3clan';
print "Hello $name, I have 1000\$ to spend, wanna join ? \n";

print "Welcome to Wonderland of \$alice\n"

print "Contact me at my email address : foo\@example.com\n"
  1. 1000$  has $ character which has been escaped using "\" character.
  2. \$alice is not variable, I just wanted to write "$alice" while using double quote.
  3. In email address , we have @ and in-order to escape and confuse perl, we escaped @ with "\".
Scenario 2 : Escaping single quote or double quote inside same type of quote
#!/usr/bin/perl

use strict;
use warnings;

print "Hi Keti, Can you meet me at \"Cafee bar\" ?  \n";

print 'Hello, I\'ll be king of perl kingdom soon!';
  1. Double quote inside double quote is escape with "\".
  2. Single quote inside single quote is escaped with "\".
This works, but it is quite hard to read such statement, do you agree ?

Well, than let's see, how to simplify it with next topic.

qq, the double-q operator

qq operator makes our reading much easier for scenario 2 problems :-

#!/usr/bin/perl

use strict;
use warnings;

my $name = 'w3clan';

print qq(The "name" is "$name"\n);

Use of qq  operators means double-quotes are not special any more in this string, so we don't need to escape them. That makes the code a lot more readable.

For some people,  qq() might look like a function call, but it is not. qq is an operator. Now,  what if you would like to include parentheses in your string ? 

#!/usr/bin/perl

use strict;
use warnings;

my $name = 'foo';

print qq(The (name) is "$name"\n);

No problem. As long as they are balanced (that is, having the same number of opening (, and closing) parentheses, and always having the opening parentheses before the corresponding closing parentheses) Perl can understand it.

Did you read coupple of line above, where we mentioned qq is an operator and not a function ? because we can use any character tp enclose the statement like :-

#!/usr/bin/perl

use strict;
use warnings;

my $name = 'foo';

print qq(The (name) is "$name"\n);

print qq{The (name) is "$name"\n};

print qq/The (name) is "$name"\n/;

print qq[The (name) is "$name"\n];

and more..

q, the single-q operator

Similar to qq there is also an operator called q. That too allows you select the delimiters of your string, but it works as a single quote ' works: It does NOT interpolate variables.

#!/usr/bin/perl

use strict;
use warnings;

my $name = 'foo';

print q(The (name) is "$name"\n);

print q{The (name) is "$name"\n};

print q/The (name) is "$name"\n/;

print q[The (name) is "$name"\n];

 


Loading ...

Related Results :

  1. String , qq , q and Escape character
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author