Here documents: how to avoid string interpolation?

Anthony Vanelverdinghe anthony.vanelverdinghe at gmail.com
Mon Feb 23 19:30:30 UTC 2015


Hi

In other languages (e.g. Bash [1], PHP [2], Ruby, Perl), this is 
typically solved by quoting the delimiter string at the start of the 
here document [3]. So I would like to propose this approach for Nashorn 
as well.
Note that some, like Bash and PHP, treat double quotes the same as 
single quotes (i.e. interpolation is disabled), whereas others, like 
Ruby and Perl, treat double quotes the same as no quotes (i.e. 
interpolation is enabled). Personally, I'm strongly in favor of the 
Bash/PHP approach to avoid confusion. So:

var a = 4;
var b = 2;
print(<<EOD)
${a}${b}
EOD

would print 42, but using print(<<"EOD") or print(<<'EOD') would print 
${a}${b}

Kind regards, Anthony

[1] http://tldp.org/LDP/abs/html/here-docs.html#EX71C
[2] 
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
[3] http://en.wikipedia.org/wiki/Here_document


On 23/02/2015 5:03, A. Sundararajan wrote:
> Hi,
>
> The following seems to work (based on code reading + testing):
>
> var str = "hello";
> var s = "$\{str} world";
> print(s); // prints ${str} world
> var s1 = "${str} world";
> print(s1) // prints "hello world"
>
> Still, I've filed a bug to clarify and document expected, supported 
> behaviour.
>
> https://bugs.openjdk.java.net/browse/JDK-8073613
>
> Thanks,
> -Sundar
>
> On Sunday 22 February 2015 04:57 PM, Anthony Vanelverdinghe wrote:
>> Hi
>>
>> How can I avoid string interpolation in here documents with Nashorn? 
>> In other words: what if the here document contains the literal string 
>> "${a}"? The user guide section on here documents [1] doesn't tell how 
>> to do this.
>> If this is not available yet, shall I create a bug report for this?
>>
>> Kind regards, Anthony
>>
>> [1] 
>> http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/shell.html#sthref26
>
>



More information about the nashorn-dev mailing list