_VERY_ basic Here Doc Question

D

dhtapp

Hi,

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>
#{Time.now}
</time>
</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?

Thanks!

- dan
 
P

Pierre Baillet

Hi,

{ Bunch of no-doubt fascinating stuff snipped ...}
if x.index( "jpclient=yes" )
session.print( <<-"JAVA_CLIENT_RAW_XML"
<timeserve_packet\>
<time>
#{Time.now}
</time>
</timeserve_packet>

Instead of:
JAVA_CLIENT_RAW_XML )

This parses:

JAVA_CLIENT_RAW_XML
)

(note the carriage return between the HERE marker and the right parent)

HTH,
 
E

Eric Schwartz

dhtapp said:
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
 
D

Daniel Carrera

Instead of:
This parses:

JAVA_CLIENT_RAW_XML
)

(note the carriage return between the HERE marker and the right parent)

Futhermore, even a space after the HERE marker will cause errors. Make
sure that the carriage return is DIRECTLY after the HERE marker.

Cheers
 
D

dhtapp

Hi Pierre,

I've tried that, with no difference in behavior.

FWIW, I'm running

ruby 1.8.0 (2003-05-26) [i386-mswin32]

- dan
 
G

Gavin Sinclair

....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.

Gavin
 
D

dhtapp

My sincere thanks to all of you. Every single one of your suggestions was
necessary for me to solve the mystery :)

- dan
 
J

Joel VanderWerf

Gavin said:
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{...}.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top