Perl Text: Quoting Without Quote Marks

In our last article we introduced you to using text in Perl. By the time we were done, there was text everywhere. But that’s okay. In this tutorial we will learn to clean that text up and put it in its place. So roll up those sleeves and prepare to get organized. No more sloppy text for you!

We left off discussing how to use special characters and escape characters like the newline and tab to determine how our text is displayed. We also talked about the effects of single and double quotes. Here, we will begin by discussing two functions you can use in place of the single and double quote.

Q…QQ…Q…I Got You!

Have you ever played a game of cops and robbers with a four year old before? You can’t win it. You point your finger at the kid and make the gun sound (q…qq…q…q) and the kid is like NEO from the Matrix. Clearly you shot him; you emptied your whole finger canon at him point blank, but the little punk swears you didn’t shoot him. Then he shoots back at you, with his finger pointing at the ceiling and swears he shot you. I mean what the hell? The next thing you know, the couch is tipped over, the kid is crying, and his parents are looking at you like you did something wrong, when clearly, you won the gun fight. And all that whiny kid has to say is: Nuh….UH!

In addition to using the single and double quotes, you can use the q and qq functions. The q function takes the place of the single quote, while qq takes the place of the double. Here it is in code:


#!/usr/bin/perl

print "nn";

print q/How’s it going?/;

print "nn";

print q|What’s that over there?|;

print "nn";

print qq[And then he said, "How are you?"];

print "nn";

This results in the print out:

  How’s it going?

 

  What’s that over there?

  And then he said, “How are you?”

You will note that we start the function with either a q or qq, and follow it with a delimiter. We then end the quote with the right-side delimiter. The left delimiter can be: <, , /, |, {, or [ and the right delimiter can be: >, /, , |, } or ].

You can also use these functions when defining a variable:


#!/usr/bin/perl

$first=q<That’s how you make a single quote>;

$second=qq{I said, "This is how you make a double quote"};

print $first;

print "nn";

print $second;

Which results in:

  That’s how you make a single quote

  I said, “This is how you make a double quote”

{mospagebreak title=Working with the Here Document…Over There}

Another way that we can quote text without using actual quotes is using the here document, which is great for long strings or paragraphs of text. You specify a here document by using the << followed by a delimiter encased in either single quotes or double quotes (single quotes if there is no variable to be interpreted, double if there is). Here is a sample in code:


#!/usr/bin/perl

print <<‘MOOSE';

We the people of the United States, in Order to form a more

perfect Union, establish Justice, insure domestic Tranquility, provide

for the common defence, promote the general Welfare, and secure the

Blessings of Liberty to ourselves and our Posterity, do ordain and

establish this Constitution for the United States of America.

MOOSE

Note that the word MOOSE can be any word you choose. This results in:

  We the people of the United States, in Order to form a more

  perfect Union, establish Justice, insure domestic Tranquility, provide

  for the common defence, promote the general Welfare, and secure the

  Blessings of Liberty to ourselves and our Posterity, do ordain and

  establish this Constitution for the United States of America.

Now try this code:


#!/usr/bin/perl

$country="United States of America";

print <<‘MOOSE';

We the people of the United States, in Order to form a more

perfect Union, establish Justice, insure domestic Tranquility, provide

for the common defence, promote the general Welfare, and secure the

Blessings of Liberty to ourselves and our Posterity, do ordain and

establish this Constitution for the $country.

MOOSE

If you run this program, you will see the following print-out:

  We the people of the United States, in Order to form a more

  perfect Union, establish Justice, insure domestic Tranquility, provide

  for the common defence, promote the general Welfare, and secure the

  Blessings of Liberty to ourselves and our Posterity, do ordain and

  establish this Constitution for the $country.

This is because we used the single quote around our delimiter instead of the double quote. The fix is simple:


#!/usr/bin/perl

$country="United States of America";

print <<"MOOSE";

We the people of the United States, in Order to form a more

perfect Union, establish Justice, insure domestic Tranquility, provide

for the common defence, promote the general Welfare, and secure the

Blessings of Liberty to ourselves and our Posterity, do ordain and

establish this Constitution for the $country.

MOOSE

Now if we run the code it will display:

  We the people of the United States, in Order to form a more

  perfect Union, establish Justice, insure domestic Tranquility, provide

  for the common defence, promote the general Welfare, and secure the

  Blessings of Liberty to ourselves and our Posterity, do ordain and

  establish this Constitution for the United States of America.

We can also store a here document in a variable:


#!/usr/bin/perl

$country="United States of America";

$con= <<"MOOSE";

We the people of the United States, in Order to form a more

perfect Union, establish Justice, insure domestic Tranquility, provide

for the common defence, promote the general Welfare, and secure the

Blessings of Liberty to ourselves and our Posterity, do ordain and

establish this Constitution for the $country.

MOOSE

print $con;

Which results in the same as above:

  We the people of the United States, in Order to form a more

  perfect Union, establish Justice, insure domestic Tranquility, provide

  for the common defence, promote the general Welfare, and secure the

  Blessings of Liberty to ourselves and our Posterity, do ordain and

  establish this Constitution for the United States of America.

{mospagebreak title=Formatting with the Here Document}

Another benefit to the here document is the ability to format your text and have it appear exactly as you typed it in. Here is a poem that we will display using the here document:


#!/usr/bin/perl

$twain= <<‘Poem';

Warm summer sun,

  Shine kindly here,

Warm southern wind,

  Blow softly here.

Green sod above,

  Lie light, lie light.

Good night, dear heart,

  Good night, good night.

Poem

print $twain;

This will display Twain’s poem like so:

Warm summer sun,

  Shine kindly here.

Warm southern wind,

  Blow softly here.

Green sod above,

  Lie light, lie light.

Good night, dear heart,

  Good night, good night.

{mospagebreak title=You Can Look But Don’t Touch}

Before we end, let’s talk about ASCII characters. As you are probably aware, computers don’t see text as we do. For instance, a computer sees a lowercase “u” as the number code 117 and the uppercase “U” as 85. There are 255 ASCII values, 93 of which are known as the visible characters. These are comprised of lowercase a-z, uppercase A-Z, the numbers 0-9, and the following characters: !”#$%&'()*+,-./:;<=>?@[]^_`{|}~.

Below is a table listing the decimal values:



Dec

Char

Dec

Char

Dec

Char

Dec

Char

033

!

059

;

085

U

111

o

034

"

060

<

086

V

112

p

035

#

061

=

087

W

113

q

036

$

062

>

088

X

114

r

037

%

063

?

089

Y

115

s

038

&

064

@

090

Z

116

t

039

065

A

091

[

117

u

040

(

066

B

092

 

118

v

041

)

067

C

093

]

119

w

042

*

068

D

094

^

120

x

043

+

069

E

095

_

121

y

044

,

070

F

096

`

122

z

045

-

071

G

097

a

123

{

046

.

072

H

098

b

124

|

047

/

073

I

099

c

125

}

048

0

074

J

100

d

126

~

049

1

075

K

101

e



050

2

076

L

102

f



051

3

077

M

103

g



052

4

078

N

104

h



053

5

079

O

105

i



054

6

080

P

106

j



055

7

081

Q

107

k



056

8

082

R

108

l



057

9

083

S

109

m



058

:

084

T

110

n



Well that’s it for this tutorial. In our next episode we will wrap up our conversation on text in Perl, and move onto working with math and numbers.

Till then…

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye