Perl: Releasing Your Inner Textuality

There are lots of way to express yourself, but with programming languages the simplest way to do that is usually text. This tutorial will walk you through ways to make text work for you in Perl. It’s the first of a three-part series, and since (as usual) we have a lot of ground to cover, let’s get started.

As you will no doubt recall, we discussed strings on a surface sort of a level a while back. Here, in this tutorial we will go more in depth. We will start off with the basics, so if you are a pro, feel free to skip over that section. If not, or if you want a refresher, sit back, take a sip of that wine in the box over there on the table, and as the great Marvin Gaye once said, let’s get it on.

Basic Printing

We learned before how to write basic text to the screen using the print command. As a refresher, here it is again:


#!/usr/bin/perl

print "My name is James Payne. ";

print "I am the world’s fattest man.";

print "My weight is equivalent to that of 17 suns. ";

print "Or 17 of Oprah’s sons. You pick. ";

print 102;

print " 102 ";

print 10+2;

print " 10+2 "

When you run this program, you get the following result:

  My name is James Payne. I am the world’s fattest man. My weight is equivalent to that of 17 suns. Or 17 of Oprah’s sons. You pick. 102 102 12 10+2

As you can see, all of our text is printed on the same line. If I had not put a space at the end of each sentence, it would all be bunched up as well. To fix this issue we can use what are known as special characters.

{mospagebreak title=Special Characters Don’t Need Helmets or Small Buses}

Special characters are preceded by a backslash() and allow you to insert what are normally "invisible" characters into your program. Sort of like Frodo when he put on the ring, only you wouldn’t think about kicking puppies or slapping ugly babies like he did. You would actually do it.

Here is a nifty table of special characters and what they do:


Special Character

What It Does

n

Creates a newline

r

Creates a carriage return (enter)

t

Creates a tab

f

Creates a formfeed

a

Creates a beep

b

Creates a backspace

e

Creates an Escape

v

Creates a vertical tab

Displays a backslash

Displays a single quote

"

Displays a double quote

So let’s go ahead and rewrite our previous code using some of the special characters to give it a more appealing feel:


#!/usr/bin/perl

print "nMy name is James Payne.n";

print "I am the world’s fattest man.nn";

print "My weight is equivalent to that of 17 suns.n";

print "Or 17 of Oprah’s sons. You pick.n";

print 102;

print "n102n";

print 10+2;

print "ttttt10+2";

This results in:

  My name is James Payne.

  I am the world’s fattest man.

 

  My weight is equivalent to that of 17 suns.

  Or 17 of Oprah’s sons. You pick.

  102

 

  102

 

  12                     10+2

We might also wish to use an escape if we want to print double and single quotes, or backslashes. Try the following code:


#!/usr/bin/perl

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

print ‘And I said, ‘Fine, fine’ ‘;

As you can see, this returns an error message (well two really). To fix this, we can use an escape character, like so:


#!/usr/bin/perl

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

print ‘And I said, ‘Fine, fine.’ ‘;

Which results in:

  And then he said, "How are you?" And I said, ‘Fine, fine.’

There is another way to fix this problem if you do not feel like using an escape character. And that is to interchange your single and double quotes, as in the following code:

#!/usr/bin/perl

print "And then he said, ‘How are you’ ";

print ‘And I said, "Fine, fine." ‘;

Which gives us the same result as before, or:

And then he said, "How are you?" And I said, ‘Fine, fine.’

Lastly, if we want to print a backslash, it will not work if we type this:


#!/usr/bin/perl

print "Look at my fine backslash: ";

However it will work if we type this:


#!/usr/bin/perl

print "Look at my fine backslash:\ ";

{mospagebreak title=Printing Variables}

We’ve learned in the past to work with variables, and I know this is a bit redundant, but keep in mind that this article is more of a cheat sheet for different ways of manipulating text.

We store text in variables and print it like so: 

!/usr/bin/perl

$a=’Apple’;

$b=’Jacks’;

print "The best cereal in the world is $a $b.";

This prints out:


  The best cereal in the world is Apple Jacks.

One note of warning: in the previous section we talked about using single and double quotes together. This method does not work the same when dealing with variables. Observe the following:


#!/usr/bin/perl

$a=’apple’;

$b=’jacks’;

print ‘The best cereal in the world is $a $b.’;

This will print out:

  The best cereal in the world is $a $b.

Which of course is not what we were hoping for.

To get it to print the values inside the variables, we have to use double quotes, like so:


#!/usr/bin/perl

$a=’Apple’;

$b=’Jacks’;

print "The best cereal in the world is $a $b.";

Which results in the proper print-out (and a very true statement):

  The best cereal in the world is Apple Jacks.

The reason why this occurs is simple enough. When Perl sees a single quotation mark, it does not interpret; when it sees double quotation marks, it does.

Note that you can store an unlimited amount of variables within your double quoted string:


#!/usr/bin/perl

$a="Hello";

$b="how";

$c="are";

$d="you";

$e="doing";

$f="today?";

$Question="$a $b $c $d $e $f";

print $Question;

Which gives us:

  Hello how are you doing today?

{mospagebreak title=More Printing Information}

Another thing to keep in mind is that you must use a backslash to print a $ symbol. Without it, Perl will try to interpret it as a variable. Behold:


#!/usr/bin/perl

print "I have $15.00 in my pocket boyee!";

If you try to run that program you will get an odd result:

  I have .00 in my pocket boyee!

Very odd indeed. Since we aren’t trying to print a variable, the proper way to write this code would be:


#!/usr/bin/perl

print "I have $15.00 in my pocket boyee!";

Which would give us the correct print out of:

  I have $15.00 in my pocket boyee!

Similarly, you cannot print out an @ symbol either. The following will not work:


#!/usr/bin/perl

print "My email address is james@james.com.";

If you used this code it would print:

  My email address is james.com.

To fix this, we again use our good old buddy the backslash:


#!/usr/bin/perl

print "My email address is james@james.com.";

Which prints out:

  My email address is james@james.com .

And lastly, we can also use escape characters and special characters on our variables. Here is an example:


#!/usr/bin/perl

$color="black";

$number="eight";

print "nnPlease find my favorite color and number below:nn";

print "tMy favorite color:ttt$colorn";

print "tMy favorite number is:ttt$numbernn";

This will print out our text and variables so that they appear like this on the user’s screen:

  Please find my favorite color and number below"


  My favorite color:               black

  My favorite number is:       eight

Well that’s all the time we have for this exciting episode. Join us next time when we continue this intriguing, gut wrenching discussion on Perl text manipulation. I look forward to seeing you.

Till then…

Google+ Comments

Google+ Comments