Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^2: quotes in Perl

by apotheon (Deacon)
on Oct 20, 2004 at 22:53 UTC ( #401009=note: print w/replies, xml ) Need Help??

in reply to Re: quotes in Perl
in thread quotes in Perl

Unfortunately, as a relative newbie to Perl, I don't know enough about here-is quoting to be able to write that section myself at this time. It's something I'll have to look into, and get back to later.

Thanks for the comment.

- apotheon
CopyWrite Chad Perrin

Replies are listed 'Best First'.
Re^3: quotes in Perl
by perlcapt (Pilgrim) on Oct 21, 2004 at 00:00 UTC

    Here Documents

    If you want to quote many lines of text literally, you use the "Here Document" notation which consists of an introductory line which has two open angles followed by a keyword, the end tag, for signalling the end of the quote. All text and lines following the introductory line are quoted. The quote ends when the end tag is found, by itself, on a line. For example, the end tag is "EOT":
    <font size="-1">#!/usr/bin/perl -w use strict; my $foo = 123.45; my $bar = "Martha Stewedprune"; print <<"EOT"; ===== This is an example of text taken literally except that variables are expanded where their variable names appear. foo: $foo bar: $bar EOT
    This example, when run, produces the following:
    ===== This is an example of text taken literally except that variables are expanded where their variable names appear. foo: 123.45 bar: Martha Stewedprune

    They way you quote, the end tag is important: like their regular quote counterparts, double-quotes allow expansion of variables and special characters, single quotes don't allow expansion. You may also have a bare, unquoted, end tag; this is equivalent to a double quote, i.e., expansion expansion.

    Some warnings:

    • The end tag specifier must follow the << without any intermediate space.
    • The actual end tag must be exactly the same as in the introduction line.
    • Don't forget that the introduction line must end with a semicolon, just like any other perl statement.
    The here document is particularly useful when embedding HTML in Perl because it increases the readability of the HTML. The quote character is printed out without any escapes. For example:
    my $url = ""; my $text = "Mother of Perl"; print <<"EOT"; <a href="$url">$text</a> EOT
    Prints just what we would hope:
    <a href="">Mother of Perl</a>

      The other thing you need to mention is that HERE-DOCS won't interpolate if single quotes are used so print <<'EOT' won't interpolate variables in the HERE-DOC, but print <<"EOT" will.
        I believe I mentioned that where I included perlcapt's suggested material in the above tutorial. He did actually allude to that fact, but I felt it needed to be slightly more obvious, and made it so in my version. His approach to the subject was well-written, though like any rough draft it needed minor editing.

        - apotheon
        CopyWrite Chad Perrin

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://401009]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2020-05-27 10:38 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (154 votes). Check out past polls.