FAQ 4.38 Why don't my <

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.


4.38: Why don't my <<HERE documents work?

    Check for these three things:

    There must be no space after the << part.
    There (probably) should be a semicolon at the end.
    You can't (easily) have any space in front of the tag.

    If you want to indent the text in the here document, you can do this:

        # all in one
        ($VAR = <<HERE_TARGET) =~ s/^\s+//gm;
            your text
            goes here

    But the HERE_TARGET must still be flush against the margin. If you want
    that indented also, you'll have to quote in the indentation.

        ($quote = <<'    FINIS') =~ s/^\s+//gm;
                ...we will have peace, when you and all your works have
                perished--and the works of your dark master to whom you
                would deliver us. You are a liar, Saruman, and a corrupter
                of men's hearts.  --Theoden in /usr/src/perl/taint.c
        $quote =~ s/\s+--/\n--/;

    A nice general-purpose fixer-upper function for indented here documents
    follows. It expects to be called with a here document as its argument.
    It looks to see whether each line begins with a common substring, and if
    so, strips that substring off. Otherwise, it takes the amount of leading
    whitespace found on the first line and removes that much off each
    subsequent line.

        sub fix {
            local $_ = shift;
            my ($white, $leader);  # common whitespace and common leading string
            if (/^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*?.*\n)+$/) {
                ($white, $leader) = ($2, quotemeta($1));
            } else {
                ($white, $leader) = (/^(\s+)/, '');
            return $_;

    This works with leading special strings, dynamically determined:

        $remember_the_main = fix<<'    MAIN_INTERPRETER_LOOP';
            @@@ int
            @@@ runops() {
            @@@     SAVEI32(runlevel);
            @@@     runlevel++;
            @@@     while ( op = (*op->op_ppaddr)() );
            @@@     TAINT_NOT;
            @@@     return 0;
            @@@ }

    Or with a fixed amount of leading whitespace, with remaining indentation
    correctly preserved:

        $poem = fix<<EVER_ON_AND_ON;
           Now far ahead the Road has gone,
              And I must follow, if I can,
           Pursuing it with eager feet,
              Until it joins some larger way
           Where many paths and errands meet.
              And whither then? I cannot say.
                    --Bilbo in /usr/src/perl/pp_ctl.c


Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.


    Copyright (c) 1997-2002 Tom Christiansen and Nathan
    Torkington, and other contributors as noted. All rights

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.

Site Timeline