Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Interactive Perl Interpreter

by tomazos (Deacon)
on Oct 11, 2005 at 08:38 UTC ( [id://499059]=perlquestion: print w/replies, xml ) Need Help??

tomazos has asked for the wisdom of the Perl Monks concerning the following question:

I've been required to learn a non-Perl Perl-like language at work to integrate a system written in it with a Perl system.

One thing I noticed is the interactive-mode of its interpreter. I think it borrowed this idea from the functional languages that it seems to have a lot of features from.

In short you get a shell-like prompt ">>> " where you can enter an expression or statement. It then evaluates it and returns a human-readable version of its value.

It also stores previous declarations and values in the current environment. For Perl it might look something like this:

>>> my $x = 3 3 >>> $x + 2 5 >>> if ($x > 2) { ... print "hello\n"; ... } hello >>> $x $x ^ Syntax error >>> $x + $x 6

I think we have stuff like this already. eg perl -e. The perl debugger. I've also seen here, people rolling there own 20 line scripts to do something similiar.

My question is what is in your opinion, the best way to setup an Interactive Perl Interpreter, and what are the options for pre-rolled ones (eg from CPAN or elsewhere)?

-Andrew.

Replies are listed 'Best First'.
Re: Interactive Perl Interpreter
by Moron (Curate) on Oct 11, 2005 at 09:29 UTC
    If it is really perl code being entered, then the bare bones are easy enough, e.g.:
    use strict; use warnings; do { print ">>> "; my $cmd = <>; my $status = eval $cmd; $status or Syntax($cmd); } while 1; sub Syntax { print STDERR "Error parsing $_[0]"; }
    The above needs some improvement, for example, eval is not as powerful as perl -e. A bigger challenge by far for a serious 'perl shell' would be to provide the advanced features, e.g. inline editing, history recall/substitution etc., that are available for e.g. ksh, so that the fledgling perl shell competes well enough with established shells.

    Update In case anyone wonders, the loop exits when the user enters 'exit', because eval 'exit' does indeed exit the interpreting program.

    -M

    Free your mind

Re: Interactive Perl Interpreter
by BerntB (Deacon) on Oct 11, 2005 at 09:19 UTC
    On my todo-stack is to look at Zoidberg, a Perl shell.

    A quick Google . That could be interesting?

      Don't forget to read the CONTEXT section in zoiduser(1) or you might be wondering why printf() is not behaving like perl-printf() and yet print() works as you might have expected.

      Right now i am looking to force zoid(1) in "PERL" context regardless of the text typed.

Re: Interactive Perl Interpreter
by blazar (Canon) on Oct 11, 2005 at 09:22 UTC
    My question is what is in your opinion, the best way to setup an Interactive Perl Interpreter, and what are the options for pre-rolled ones (eg from CPAN or elsewhere)?
    perl -de1
Re: Interactive Perl Interpreter
by Akhasha (Scribe) on Oct 12, 2005 at 01:46 UTC
    After playing around with the Perl SHell "psh", I've settled on Zoidberg. Not as a login shell mind you, most commonly I fire it up when I've created a new class and want to test out how it would be used from a script.

    Given the rich (pungent?) heritage of Perl it makes some sense using it to replace the shell that it inherits so much from.
Re: Interactive Perl Interpreter
by Your Mother (Archbishop) on Oct 11, 2005 at 22:34 UTC

    Simplistic (not as deep as the interactive debug: perl -de 42) but easy to work with:

    perl -wlne 'eval; $@ and warn $@'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://499059]
Approved by muntfish
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 16:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found