Perl Eof Variable Assignment


Next:Printing to a String Up:Basic I/O Previous:Changing the Destination of   Contents

Printing Blocks of Text

When you need to print several lines of text, it becomes tiresome to either call the print function for each individual line or to insert characters after every line, and your program may become disorganized due to the presence of long character strings. This problem arises when, for example, you are generating form letters or mutltiple pages of html text from inside a perl program, or you want your perl program to print out a detailed help message. To solve this problem, perl borrows a concept from shell scripts known as a ``here-is'' document. You inform perl that the text that follows should be treated as a single block by using two less-than signs (), immediately followed by an arbitrary string of your choice. Since this is actually a perl statement, it must be terminated by a semi-colon. Then you can provide perl with as much text as you want, spread out across as many lines as necessary, signalling to perl that you're done by providing a line with nothing but the string you chose on the first line, starting in the first column, and with no semicolon. While there is no need to do so, many perl programmers use the string (end of file) as a delimiter for here-is documents.

This construction is most often used with the statement, but you can also use it to set variables equal to blocks of text (although it won't work in the debugger). Thus, to put three lines of text into the variable , you could use the following code:

$threelines = <<EOF; Here is the first line Now here's the second Finally the third EOF

Since variable interpolation takes place inside of here-is documents, it can be used to generate documents which are basically similar except for a few strings. To illustrate, suppose we wish to produce a memo to a variety of people informing them of their account balances. Further suppose that we have a hash called , with keys for each of the people we wish to send the memo. We could generate the memos with code like the following:

$fh = new IO::File; foreach $p (keys(%balances)){ $fh->open("$p.memo","w"); print $fh <<EOF; To: $p Re: Your Account Balance This memo is to inform you that your account balance is currently $balances{$p}. Please send your payment as soon as possible. Thank you. EOF }

The quoting operators (Section 3.2) also provide a convenient solution to problems such as this. The main difference is that, if you start the block of text on the line after the quoting operator, an empty line will appear at the start of the block, whereas the here-is construction requires that the block of text begins on the line following its invocation.



Next:Printing to a String Up:Basic I/O Previous:Changing the Destination of   Contents Phil Spector 2002-10-18

HERE documents

Introduction

If you're tempted to write multi-line output with multiple statements, because that's what you're used to in some other language, consider using a HERE-document instead.

Inspired by the here-documents in the Unix command line shells, Perl HERE-documents provide a convenient way to handle the quoting of multi-line values.

So you can replace this:

print "Welcome to the MCG Carpark.\n"; print "\n"; print "There are currently 2,506 parking spaces available.\n"; print "Please drive up to a booth and collect a ticket.\n";

with this:

print <<'EOT'; Welcome to the MCG Carpark. There are currently 2,506 parking spaces available. Please drive up to a booth and collect a ticket. EOT

The in this example is an arbitrary string that you provide to indicate the start and end of the text being quoted. The terminating string must appear on a line by itself.

Quoting conventions.

The usual Perl quoting conventions apply, so if you want to interpolate variables in a here-document, use double quotes around your chosen terminating string:

print <<"EOT"; Welcome to the MCG Carpark. There are currently $available_places parking spaces available. Please drive up to booth and collect a ticket. EOT

Note that whilst you can quote your terminator with or , you cannot use the equivalent and operators. So this code is invalid:

# This example will fail print <<qq(EOT); Welcome to the MCG Carpark. There are currently $available_places parking spaces available. Please drive up to booth and collect a ticket. EOT

The terminating string

Naturally, all of the text you supply to a here-document is quoted by the starting and ending strings. This means that any indentation you provide becomes part of the text that is used. In this example, each line of the output will contain four leading spaces.

# Let's indent the text to be displayed. The leading spaces will be # preserved in the output. print <<"EOT"; Welcome to the MCG Carpark. CAR PARK FULL. EOT

The terminating string must appear on a line by itself, and it must have no whitespace before or after it. In this example, the terminating string is preceded by four spaces, so Perl will not find it:

# Let's indent the following lines. This introduces an error print <<"EOT"; Welcome to the MCG Carpark. CAR PARK FULL. EOT Can't find string terminator "EOT" anywhere before EOF at ....

Assignment

The here-document mechanism is just a generalized means of quoting text, so you can just as easily use it in an assignment:

my $message = <<"EOT"; Welcome to the MCG Carpark. CAR PARK FULL. EOT print $message;

And don't let the samples you've seen so far stop from considering the full range of possibilities. The terminating tag doesn't have to appear at the end of a statement.

Here is an example from that conditionally assigns some text to .

$msg = <<EOF unless $configpm =~ /MyConfig/; # This is CPAN.pm's systemwide configuration file. This file provides # defaults for users, and the values can be changed in a per-user # configuration file. The user-config file is being looked for as # ~/.cpan/CPAN/MyConfig.pm. EOF

And this example from Module::Build::PPMMaker uses a here-document to construct the format string for :

$ppd .= sprintf(<<'EOF', $perl_version, $^O, $self->_varchname($build->config) ); <PERLCORE VERSION="%s" /> <OS NAME="%s" /> <ARCHITECTURE NAME="%s" /> EOF

See Also

perldoc -q "HERE documents"perldoc perlop (see the <<EOF section). Perl for Newbies - Lecture 4

[Top]

0 thoughts on “Perl Eof Variable Assignment”

    -->

Leave a Comment

Your email address will not be published. Required fields are marked *