2012-03-28

perl command interpreter

Since every fancy new programming language seems to have something called a "command interpreter" which even seems usefull I started thinking how difficult it would be to write something like this for perl.
Turns out a very simple version of this is quite easy to do in perl it'self :)
It's not as mature as its counterparts from ruby or python, but I think for the purpose it serves it's enough.

Oneliner:
perl -e "while(<>){eval;}"

As a script with return value printing:

#!/usr/bin/perl -w
my $command;
my $returnval;
print "welcome to flatline's perl shell.\nPlease do not use \$command or \$returnval as they are being used.\nperlshell> ";
while($command = <STDIN>){
         $returnval = eval $command;
         print "\nreturned: " . $returnval . "\nperlshell> ";
}

No comments:

Post a Comment