_VERY_ basic Here Doc Question

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

Thanks!

  • dan

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,

···

On Thu, Dec 04, 2003, dhtapp wrote:
--
Pierre Baillet
          Support the Debian GNU/Linux Project (http://www.debian.org)

“dhtapp” dhtapp@cox.net writes:

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.

Gavin

Instead of:

JAVA_CLIENT_RAW_XML )

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

···


Daniel Carrera | Top 100 things you don’t want the sysadmin to say…
PhD student. |
Math Dept. UMD | 84. Where’s the GUI on this thing?

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

“Pierre Baillet” oct@zoy.org wrote in message
news:20031204011234.GH24451@zoy.org

···

This parses:

JAVA_CLIENT_RAW_XML
)

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

HTH,

Pierre Baillet
Support the Debian GNU/Linux Project (http://www.debian.org)

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

  • dan

Gavin Sinclair wrote:

…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{...}.

Hi Pierre,

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

Check for a blank space after the HERE marker.

···


Daniel Carrera | Top 100 things you don’t want the sysadmin to say…
PhD student. |
Math Dept. UMD | 84. Where’s the GUI on this thing?