Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yup, the multiple here docs on a line thing is also in the POSIX spec for sh. I had no idea that was possible before implementing the shell!

And I'm pretty sure it does NOT appear in the million lines of shell code that I've parsed. They use here docs in weird ways, but they don't have multiple here docs on a line.

This led me to find the algorithm for parsing here docs, which surprisingly all shells I've tested support, even though almost no programs use it.

http://www.oilshell.org/blog/2016/10/17.html

The algorithm is: process the here docs in a post-order traversal of the AST.

There is an example in my blog with 3 here docs, with one of them being in the while CONDITION (which is itself a command):

    while cat <<EOF1; read line; do echo "  -> read '$line'"; cat <<EOF2; done <<EOF3
    ...
In shell at least, you can put the ENTIRE Program on one line. Change all the newlines to semi-colons. You could have 1000 here docs in a program. So you could put 1000 here docs beginnings on one line, and then you process the ends in that specific order.

Ruby must have borrowed this from shell... I guess Matz knows shell REALLY WELL.



I did not know that about sh. And I agree with the sibling comment that it's probably been inherited via Perl (which I also didn't realise had it, but then that fits totally into every stereotype about Perl...) - Ruby has a whole lot of Perl-ism in it, most of which we try not to talk about and pretend aren't there ;)

In terms of influences, Ruby is basically what happens if Perl and Smalltalk has a prettier baby.


Ruby borrowed them from Perl, which borrowed them from shell. I used mutliple heredocs once or twice in both perl and shell myself, but they break syntax highlighting, so it's hard to keep using them.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: