Home arrow Perl Programming arrow Page 2 - Perl: Releasing Your Inner Textuality

Special Characters Don't Need Helmets or Small Buses - Perl

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.

TABLE OF CONTENTS:
  1. Perl: Releasing Your Inner Textuality
  2. Special Characters Don't Need Helmets or Small Buses
  3. Printing Variables
  4. More Printing Information
By: James Payne
Rating: starstarstarstarstar / 1
February 25, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.\n\n";

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

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

print 102;

print "\n102\n";

print 10+2;

print "\t\t\t\t\t10+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:\\ ";



 
 
>>> More Perl Programming Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: