Thursday, June 24, 2010

Updated interactive perl script

This is still just a quick script to test out perl concepts before trying to write them into a program. It isn't fancy but it is functional.

Look for more updates as I build this out a bit more:
#!/usr/bin/env perl

use Term::ReadLine;
use strict;
use warnings;

my $prompt = "> ";

my $term = new Term::ReadLine("iPerl -- interactive perl");

while (defined ($_ = $term->readline($prompt))) {
chomp;

next if (/^\s$/);
last if (/^(?:q|quit|exit|logout)$/);

my $output = eval $_ or print $!;

print "\nOUT: $output\n" if $output;
}

print "\n";

Wednesday, June 23, 2010

how to do ipython in perl...

I've been writing a lot of python code lately and I really enjoy having that instant trial and error you can get by using an interactive code shell. (IPython and a half dozen other tools allow this).

I had to switch to perl on a project and went looking for a similar tool. I wasn't able to find much out there but I did stumble on a quick solution.

perl -ne 'eval $_ ; print ">> "'


This will loop over all input from standard in and attempt to evaluate it. The only downside is you won't be able to write anything that takes multiple lines (for, while, try, or functions). But it is a lot easier than writing perl oneliners over and over again to test out theories.