Tuesday, 15 May 2012

Perl REPL

While trying to translate a Perl script to a ruby script, I found the necessity of testing things on the interpreter. Ruby provides IRB for that. If you are using Ruby on Rails, you might prefer Rails console over IRB. However, I could not find anything similar in Perl. I could do some tasks from the command line as follows:

perl -e "print 'Hello World';"

However, whenever I was trying any assignment, it was not working. For example,

perl -e "$k = 'Hello World';print $k;"

did not work. I later came to know it was because bash was gulping $k because I had used a double-quoted string. Using a single-quoted string works fine.

perl -e '$k = "Hello"; print $k;'

A quick search showed that I can use Devel::REPL for this purpose. So, I obtained it from CPAN.


cpan -i Devel::REPL

I prefer using the Perl REPL interpreter using the re.pl script.

[blog@domain ~]$ re.pl
$ my $k = "Hello World"
Hello World$ print $k;
1$ Hello World
$ 

No comments: