Matt Follett

Perl to Groovy Mappings (an Introduction)

Recently I took a job with a startup that is using Groovy as their main language. I find the language to be similar to several other languages and they have some comparisons on their site, however one comparison they are missing is to Perl. Since I’m learning Groovy anyway and the two are sufficiently similar that I thought I’d give it a shot.

Before I start with the articles there are a few important things to note about Groovy:

  1. Groovy autoboxes, as such it is completely legitimate to write `”I like cheese”.someMethodOnStrings()`.
  2. Groovy automatically makes a variable named `it` as the only parameter for lambda function if no parameter list is given, meaning that: `def foo = { it * 2}` is similar to `def foo = { it -> it * 2}`.
  3. Groovy’s syntax provides an alternate way to list parameters for a method call when the last parameter is a lambda function meaning that `someData.someMethod(1,2, { it + 2})` is the same as `someData.someMethod(1,2) { it + 2}`, this is useful for methods that loop over lists, like `each`.