Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Groovy Quick Start


[ 28 ]

The Groovy shell – groovysh


The Groovy shell is a useful command-line tool for trying out snippets of Groovy
code interactively. The shell allows you to enter Groovy code line by line and
execute it. We can run the groovysh command and immediately start entering
Groovy statements:


$ groovysh
Groovy Shell (2.4.4, JVM: 1.8.0_51)
Type ':help' or ':h' for help.
--------------------------------------------------------------------groo
vy:000> "Hello, World!"
===> Hello, World!
groovy:000>


The Groovy shell evaluates each line and outputs the return value of the statement.
The preceding statement contains just one instance of a string, so the string
Hello, World! is returned. By contrast, our single line Hello script outputs
Hello, World!, but returns null, as follows:


groovy:000> println "Hello, World!"
Hello, World!
===> null
groovy:000>


This is an important point to remember if you make use of the Groovy shell, as
it sometimes can be the cause of unexpected error messages in otherwise correct
scripts. The Groovy shell will try to interpret and print the return value of your
statement irrespective of whether it makes sense to do so or not.


With the Groovy shell, statements can span more than one line. Partially complete
Groovy statements are stored in a buffer until completion. You can use the :display
command to output a partially complete statement, as follows:


groovy:000> class Hello {
groovy:001> :display
001> class Hello {
groovy:001> String message
groovy:002> :display
001> class Hello {
002> String message
groovy:002> }
===> true
groovy:000>


http://www.ebook3000.com
Free download pdf