title: Using GroovyShell as "expression evaluator/engine" (or: How to reuse GroovyShell)
body: <p>I'm using <code>GroovyShell</code> as an "expression evaluator/engine" inside my program. It accepts two inputs: (a) one or more init scripts (b) user-defined script. Both are then concatenated at runtime as big chunk of script (text) and feed to the shell.</p>
  
  <pre><code>String initScripts = getFromDB()
  String userScript = getFromUser()
  
  def shell = new GroovyShell()
  output = shell.evaluate(initScripts + userScript)
  </code></pre>
  
  <p>The above code will run in a loop, where the contents of <code>userScript</code> will vary.</p>
  
  <p>So far, <code>initScripts</code> only contain variable definitions (e.g. <code>def $yyyy = new Date().format('yyyy')</code>) which might be referenced in <code>userScript</code> (e.g. <code>print "$yyyy 001"</code>).</p>
  
  <p>Is there any more efficient approach for this? (E.g. reusing the shell, how?) Because right now it's <em>very</em> slow.</p>
  
  <p><strong>Edit:</strong> Groovy is a must. Please don't recommend other scripting engine.</p>
  
  <p><strong>Edit:</strong> I'm thinking whether GroovyShell can do this (pseudo-code):</p>
  
  <pre><code>def shell = new GroovyShell()
  shell.evaluate(initScripts)
  
  for each userScript in DB {
      shell.put(userScript )
      def result = shell.evaluateThat()
      println "Result is $result"
  }
  </code></pre>
  
  <p>Is this possible? (Last time I googled it's not possible, but I'm hoping I'm wrong)</p>