I’m dinking with some examples in D&A’s book (specifically, a socket-level
time server), and I just tried replacing a slew of output statements with
what I thought would be a well-formed here document.
Here’s the offending code:
while ( session = server.accept )
x = "Request: #{session.gets}"
print x
session.print( “HTTP/1.1 200/OK\r\n” )
session.print(“Content-type: text/html\r\n\r\n” )
{ Bunch of no-doubt fascinating stuff snipped …}
if x.index( “jpclient=yes” )
session.print( <<-“JAVA_CLIENT_RAW_XML”
<timeserve_packet>
#{Time.now}
</timeserve_packet>
JAVA_CLIENT_RAW_XML )
next
end
{ &etc. }
…but the here doc doesn’t parse. Can someone please take two nanoseconds
to point out the obvious?
if x.index( “jpclient=yes” )
session.print( <<-“JAVA_CLIENT_RAW_XML”
JAVA_CLIENT_RAW_XML )
next
end
{ &etc. }
…but the here doc doesn’t parse. Can someone please take two nanoseconds
to point out the obvious?
Gladly. It should be:
session.print( <<-“BLAH” )
…
stuff
…
BLAH
My rule of thumb with here-docs is to think of it like the '<<DELIM’
part is wholly replaced by everything starting on the next line until
DELIM. So you should close parens, add semicolons, whatever, after
the ‘<<DELIM’ and THEN start the here-doc contents.
-=Eric
···
–
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
– Blair Houghton.
…but the here doc doesn’t parse. Can someone please take two
nanoseconds to point out the obvious?
Try this:
print(<<-EOF)
blah blah blah
EOF
Lessons:
enclose <<-EOF in brackets; don’t wrap entire doc in brackets
start with simple end-document-marker, then work your way up
if you want to
don’t use quotes on document marker
The first point is probably the only thing that matters in your case. I
listed the others because you should try the simple things to eliminate
possible causes of error.
…but the here doc doesn’t parse. Can someone please take two
nanoseconds to point out the obvious?
Try this:
print(<<-EOF)
blah blah blah
EOF
Lessons:
enclose <<-EOF in brackets; don’t wrap entire doc in brackets
start with simple end-document-marker, then work your way up
if you want to
don’t use quotes on document marker
- keep in mind the %{...} notation if you really want to put
parens around your string:
print(%{your
string
blah blah blah
})
If you need single-quote behavior, there is $q{...}.