4

below code can not run

def map = [name:"Test :: ( %2f %25 \$ * & ! @ # ^)"]
String s = map.inspect()
println Eval.me(s)

get error:

Script1.groovy: 1: illegal string body character after dollar sign; 

solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 1, column 30.
   ["name":"Test :: ( %2f %25 $ * & ! @ # ^)"]

but if string contain other special char like \", it works correctly. any way, how to walk around? it's emergency for me

Xilang
  • 1,513
  • 3
  • 18
  • 36
  • I tried what you are doing. You may very well have found a bug within Groovy's inspect method. *However*, that being said, **what are you really trying to accomplish?** The `Eval` method should never be used the way you are showing there - it's kind of a general rule that [eval is evil](http://stackoverflow.com/questions/86513/why-is-using-javascript-eval-function-a-bad-idea) (yes, that's JS but the same issues apply to all dynamic languages). – OverZealous Sep 14 '11 at 22:39
  • We want to transfer data between two computers. The data structure is Map, so I call inspect() in one computer and pass the string to the other computer, it call eval.me to re-build Map structure – Xilang Sep 15 '11 at 03:25
  • OK, see below for my response. – OverZealous Sep 15 '11 at 03:38
  • 1
    Possible duplicate of [Gradle Copy files and expand only some of them and/or ignore dollar signs in others](http://stackoverflow.com/questions/25152045/gradle-copy-files-and-expand-only-some-of-them-and-or-ignore-dollar-signs-in-oth) – Boris Lopez Feb 22 '17 at 15:22
  • See my response here: http://stackoverflow.com/questions/25152045/gradle-copy-files-and-expand-only-some-of-them-and-or-ignore-dollar-signs-in-oth/42395410#42395410 – Boris Lopez Feb 22 '17 at 15:22

4 Answers4

2

(In response to follow up above)

OK, if you just want to exchange information, then you should use a Data Interchange Format, such as XML or JSON. I recommend JSON because it's lightweight, fast, and really easy to use:

Computer 1

def map = [name:"Test :: ( %2f %25 \$ * & ! @ # ^)"]
def json = new groovy.json.JsonBuilder()
json(map)
println json.toString()

Computer 2

def incoming = '{"name":"Test :: ( %2f %25 $ * & ! @ # ^)"}'
def jsonInput = new groovy.json.JsonSlurper()
def map = jsonInput.parseText(incoming)
println map

Please note that these require Groovy 1.8.0 or newer to function. There are plenty of examples for older versions of Groovy, and Grails has it's own parsers built-in as well.

OverZealous
  • 39,252
  • 15
  • 98
  • 100
0

I just tested the following and it worked for me

> a = ["guy":"mogr \$ abi"]
Eval.me(a.inspect())["guy"]
mogr $a bi
guy mograbi
  • 27,391
  • 16
  • 83
  • 122
0

Use single quotes round your string:

def map = [name:'Test :: ( %2f %25 \$ * & ! @ # ^)']

When you use double quotes, the dollar char is used for templating

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Thanks, but my problem is the string is not created by me, it's created by the inspect() method.even I define map as you said, when call inspect() method, groovy will convert the string to "Test :: ( %2f %25 $ * & ! @ # ^)" – Xilang Sep 14 '11 at 07:23
  • @Xilang Can you try using the [dollar-slash string delimiter](http://mrhaki.blogspot.com/2011/04/groovy-goodness-new-dollar-slashy.html) like: `def map = [name:$/Test :: ( %2f %25 \$ * & ! @ # ^)/$]`? – tim_yates Sep 14 '11 at 07:46
  • Our project use groovy 1.7.8, so sad – Xilang Sep 15 '11 at 03:13
0

Use single quotes and a double backslash ,like,

def map = [name:'Test :: ( %2f %25 \\$ * & ! @ # ^)']
String s = map.inspect()
println Eval.me(s)
aldrin
  • 4,482
  • 1
  • 33
  • 50
  • But 'Test :: ( %2f %25 \\$ * & ! @ # ^)' is not the string I want. I want string $ not \$. – Xilang Sep 15 '11 at 03:08
  • After the .inspect(), you will get the string you wanted. If you want string $, then def map = [name:'Test :: ( %2f %25 \$ * & ! @ # ^)']. If you want \$, then def map = [name:'Test :: ( %2f %25 \\$ * & ! @ # ^)'] .Anyway, it looks like you are considering an XML/JSON solution. – aldrin Sep 15 '11 at 07:55