Jo-Herman Haugholt
Jo-Herman Haugholt

Reputation: 462

How to avoid evaluating an GString

I'm working on extending a legacy script system using groovy. The source scripts are "java-like", so it mostly parses as a groovy script with a little pre-processing.

I'm using invokeMethod() and missingMethod() to pass-through the legacy code, enabling me to use closures and other groovy features to enhance the scripts. However, the original script uses strings of the type "$foo" to refer to variables. When a legacy method is caught by missingMethod(), I need it to not evaluate this string as a GString, but simply outputting it verbatim. Is this possible in any way?

Upvotes: 4

Views: 3867

Answers (1)

rdmueller
rdmueller

Reputation: 11062

there are two way to escape the $foo:

  1. escape the '$' as '\$'
  2. use ' instead of " as string delimiter

example:

 def test = "bad"
 def s0 = "$test"
 def s1 = "\$test"
 assert s1 != s0
 def s2 = '$test'
 assert s2 == s1
 println s0
 println s1
 println s2

So I guess you have to use your preprocessor in order to escape your strings

Upvotes: 7

Related Questions