Home arrow Perl Programming arrow Page 2 - Perl Text: Quoting Without Quote Marks

Working with the Here Document...Over There - Perl

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!

TABLE OF CONTENTS:
  1. Perl Text: Quoting Without Quote Marks
  2. Working with the Here Document...Over There
  3. Formatting with the Here Document
  4. You Can Look But Don't Touch
By: James Payne
Rating: starstarstarstarstar / 2
March 03, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> 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: