My Groovy Vs Perl
Currently I’m working at the Telecommunication Company (Bakrie Telecom). I’m a staff of IT Billing Prepaid Development Department. The main function of this department is to build a variety of interfaces between IT Application whether it is build by other IT teams or by the third party vendors, to Bakrie Telecom ‘s Network Element such as SMSC (Short Message Service Center), IN (Intelligent Network), HLR (Home Locator Register) and any others standard TELCO Network Element.
My Manager is a PERL Hacker. He can do his magic with just a few of key stroke to accomplish variety of job or script, for example to parse the large CDR files, make a network connection to the Network Elements (for provisioning process) or connect to the Database. So in my first career in this company, I have to read preexisting of codes and I’m sure I got a lot of thing from it. But I don’t know why something missing in my mine. PERL is not really comfortable to program with (for me). Maybe, this is because my strong background as a Java programmer. So after about six months working for this company, I decided to write some of my new development jobs using Java Technology.
But after some development job has been done using my favorite Java Technology, I became to realize that there are some difficulties when I had a kind of development jobs that has a lot of parsing tasks. As we know, it’s very hard or maybe complex to accomplish such a job in Java programming language because of the lack of expressiveness of this language.
But now, the situations are totally different, I got a Groovy Programming language as second standard programming language in Java platform. So with this language I can accomplish a lot of job that is hard to do with Java Programming language with more enjoyment and fun.
In this article I want to show some examples of how Groovy programming Language can be more expressive than PERL.
1. Read a text file
In this section assumed that we’re going to dump a text file located at local directory c:/mylovepoet.txt
This is my code in groovy:
/**
Reading a text file located at c:/mylovepoet.txt using Groovy
*/
new File(“c:/mylovepoet.txt”).eachLine{ line ->
println line
}
This is my code in PERL
/**
Reading a text file located at c:/mylovepoet.txt using PERL
*/
open(INPUT, “c:/mylovepoet.txt”);
while(<INPUT>) {
$line = $_;
$line =~ s/\r|\n//gi;
print “$line\n”;
}
close(INPUT);
2. List Manipulation
In this example we have the list of programming language names, from this list we want to extract the programming language name that contains special characters such as ‘+’ or ‘#’. We can accomplish this job by the following Groovy and PERL script:
/**
List usage in Groovy
*/
pl_list = [‘B’, ‘C’, ‘C++’, ‘JAVA’, ‘C#’]
pl_with_special_char_list = pl_list.grep(/^\w(\+|#)+\w*$/)
and this is my script in PERL
/**
List usage in PERL
*/
@pl_list = (‘B’, ‘C’, ‘C++’, ‘JAVA’, ‘C#’);
@pl_with_special_char_list = ();
$i = 0;
for($pl (@pl_list)) {
if($pl =~ /^\w(\+|#)+\w*$/) {
$pl_with_special_char_list[$i] = $
$i++;
}
}
This article have no intention to prove that Groovy is better than PERL, but this article just want to show us that Groovy can be more expressive than PERL.
Very interesting- I’m a Perl and Java programmer who’s just now learning Groovy and Grails so I came across your blog while looking for other comparisons between the languages. What I’m finding interesting is the way that Groovy uses the word, or at least the concept, “closure” a little more loosely than Perl does- in both examples you’re using a Groovy closure, effectively a subroutine call, where Perl uses a code block, more like Java or C. Groovy’s closures feel more like anonymous subroutines in Perl, and a Perl ‘closure’ specifically has data attached to it (generally a lexical variable that’s only in scope for the closure and nowhere else- see http://www.perl.com/pub/a/2002/05/29/closure.html if you like gory details
)
Your Perl is fine, just a little wordier than it needs to be while still not being line-noise Perl golf.
#1 could be
open(INPUT, ‘<’, “c:/mylovepoet.txt”);
while(my $line = ) {
chomp $line;
print $line, “\n”;
}
close INPUT;
#2 can also be trimmed quite a bit, using pretty much the same construct as Groovy:
my @pl_list = qw/ B C C++ JAVA C#/;
my @pl_with_special_char_list = grep (/[#+]/, @pl_list);
#You could inline @pl_list, but you could have done the same in Groovy.
Scott
January 8, 2009 at 9:56 pm
what a nice scott. Thanks for your commnet.
adpjhype
January 9, 2009 at 3:12 pm
On comparrison 1, I think you might be doing a little more formatting in Perl than you are in Groovy (you’re stripping CR/LF and then re-adding a linefeed in Perl whereas in Groovy you just leaving the existing line termination in place).
Probably more apples to apples would be this in Perl:
open(INPUT, “mylovepoet.txt”);
foreach(){print $_}
close INPUT;
None the less, I see your point on some of the “handiness” of Groovy and I am pulling it down for a test spin right now
-Matt
Matthew Twomey
February 6, 2009 at 6:06 am
#1 Can be writen more like Groovy
use File::Slurp;
print $_ for read_file(“mylovepoet.txt”);
-Grian
Grian
May 8, 2009 at 9:10 am
What Matthew Twomey meant to say is :
open(INPUT, “mylovepoet.txt”);
print foreach <INPUT>;
close INPUT;
My opinion is that both Groovy and Perl are nice languages, although Perl allows more ways to express one thing (that comes with advantages and disadvantages).
Viorel Stirbu
July 23, 2009 at 12:42 pm