4

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?

Dónal
  • 185,044
  • 174
  • 569
  • 824
Jo-Herman Haugholt
  • 462
  • 1
  • 5
  • 15

1 Answers1

7

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

rdmueller
  • 10,742
  • 10
  • 69
  • 126