Heredocs and white space

J

Jack Christensen

Is there some reason heredocs fail if there is white space after the
closing string? Until I realized what was happening this caused me a
good bit of trouble. Invisible characters shouldn't cause stuff to fail.

Example:
s = <<-HEREDOC
bla bla bla
HEREDOC

This fails with:

heredoc.rb:3: can't find string "HEREDOC" anywhere before EOF
heredoc.rb:1: parse error, unexpected $, expecting tSTRING_CONTENT or
tSTRING_DB
EG or tSTRING_DVAR or tSTRING_END

Delete the spaces at the end of the 3rd line and it works.

I looked through the relevant PickAxe sections and didn't find anything.

Jack
 
D

dblack

Hi --

Is there some reason heredocs fail if there is white space after the closing
string? Until I realized what was happening this caused me a good bit of
trouble. Invisible characters shouldn't cause stuff to fail.

Example:
s = <<-HEREDOC
bla bla bla
HEREDOC
This fails with:

heredoc.rb:3: can't find string "HEREDOC" anywhere before EOF
heredoc.rb:1: parse error, unexpected $, expecting tSTRING_CONTENT or
tSTRING_DB
EG or tSTRING_DVAR or tSTRING_END

Delete the spaces at the end of the 3rd line and it works.

I looked through the relevant PickAxe sections and didn't find anything.

The thing is, the mechanism depends on an exact match. Here's an
unlikely but possible example of why it might matter. Line 10 is
"EOM"; line 12 is "EOM ".

irb(main):008:0> a,b = <<EOM, <<"EOM "
irb(main):009:0" abc
irb(main):010:0" EOM
irb(main):011:0" def
irb(main):012:0" EOM
=> ["abc\n", "def\n"]


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
D

dblack

Hi --

Hi --

Is there some reason heredocs fail if there is white space after the
closing string? Until I realized what was happening this caused me a good
bit of trouble. Invisible characters shouldn't cause stuff to fail.

Example:
s = <<-HEREDOC
bla bla bla
HEREDOC This fails with:

heredoc.rb:3: can't find string "HEREDOC" anywhere before EOF
heredoc.rb:1: parse error, unexpected $, expecting tSTRING_CONTENT or
tSTRING_DB
EG or tSTRING_DVAR or tSTRING_END

Delete the spaces at the end of the 3rd line and it works.

I looked through the relevant PickAxe sections and didn't find anything.

The thing is, the mechanism depends on an exact match. Here's an
unlikely but possible example of why it might matter. Line 10 is
"EOM"; line 12 is "EOM ".

irb(main):008:0> a,b = <<EOM, <<"EOM "
irb(main):009:0" abc
irb(main):010:0" EOM
irb(main):011:0" def
irb(main):012:0" EOM => ["abc\n", "def\n"]

Actually I don't think I answered your question. Looking at your
example again, it does look odd. "HEREDOCabc" would be ignored,
whereas "HEREDOC " causes a failure. I'm not sure why.


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
J

John Wilger

Actually I don't think I answered your question. Looking at your
example again, it does look odd. "HEREDOCabc" would be ignored,
whereas "HEREDOC " causes a failure. I'm not sure why.

Seems to be specific to the "<<-" notation.

irb(main):001:0> s = <<HEREDOC
irb(main):002:0" bla bla bla
irb(main):003:0" HEREDOC
irb(main):004:0" HEREDOC
=> "bla bla bla\nHEREDOC \n"


with a space at the end of line 3 works as expected.

--
Regards,
John Wilger
http://johnwilger.com

-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland
 
F

fprimus

Jack said:
Example:
s = <<-HEREDOC
bla bla bla
HEREDOC
This fails with:

heredoc.rb:3: can't find string "HEREDOC" anywhere before EOF
heredoc.rb:1: parse error, unexpected $, expecting tSTRING_CONTENT or
tSTRING_DB
EG or tSTRING_DVAR or tSTRING_END

Delete the spaces at the end of the 3rd line and it works.

It works then because "HEREDOC" is equal to "HEREDOC", when the space at
the end is present it's "HEREDOC ". I just tried it at the command line
using bash and when there was a space at the end it did not blow up, it
merely included the "HEREDOC " as the last line. Removing the space
caused it to work correctly. I did not try it under perl or php.


--

Music: http://www.myspace.com/acidredux

Links: http://del.icio.us/furlan

Home: http://thispaceavailable.uxb.net/index.html

We are here to laugh at the odds and live our lives so well that Death
will tremble to take us.
-- Charles Bukowski
 

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,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top