perlfaq nodetype
faq_monk
<P>
Check for these three things:
<OL>
<LI><STRONG><A NAME="item_There_must_be_no_space_after_the">There must be no space after the << part.</A></STRONG>
<LI><STRONG><A NAME="item_There_probably_should_be_a_sem">There (probably) should be a semicolon at the end.</A></STRONG>
<LI><STRONG><A NAME="item_You_can_t_easily_have_any_spac">You can't (easily) have any space in front of the tag.</A></STRONG>
</OL>
<P>
If you want to indent the text in the here document, you can do this:
<P>
<PRE> # all in one
($VAR = <<HERE_TARGET) =~ s/^\s+//gm;
your text
goes here
HERE_TARGET
</PRE>
<P>
But the
<FONT SIZE=-1>HERE_TARGET</FONT> must still be flush against the margin. If
you want that indented also, you'll have to quote in the indentation.
<P>
<PRE> ($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
FINIS
$quote =~ s/\s*--/\n--/;
</PRE>
<P>
<FONT SIZE=-1>A</FONT> 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 off. Otherwise, it takes the
amount of leading white space found on the first line and removes that much
off each subsequent line.
<P>
<PRE> sub fix {
local $_ = shift;
my ($white, $leader); # common white space and common leading string
if (/^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*\1\2?.*\n)+$/) {
($white, $leader) = ($2, quotemeta($1));
} else {
($white, $leader) = (/^(\s+)/, '');
}
s/^\s*?$leader(?:$white)?//gm;
return $_;
}
</PRE>
<P>
This works with leading special strings, dynamically determined:
<P>
<PRE> $remember_the_main = fix<<' MAIN_INTERPRETER_LOOP';
@@@ int
@@@ runops() {
@@@ SAVEI32(runlevel);
@@@ runlevel++;
@@@ while ( op = (*op->op_ppaddr)() ) ;
@@@ TAINT_NOT;
@@@ return 0;
@@@ }
MAIN_INTERPRETER_LOOP
</PRE>
<P>
Or with a fixed amount of leading white space, with remaining indentation
correctly preserved:
<P>
<PRE> $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
EVER_ON_AND_ON
</PRE>
<P>