http://www.perlmonks.org?node_id=441777

(Hope this is the right spot for this!) Pythonists complain that Perl doesn't have an interactive mode, but of course you can easily make one. However other examples I have seen were 20 line scripts, a little while ago I made up this one liner (which I tend to stash in a minimal shell script called ip, on every machine I use):
perl -ne 'print join(",", eval),"\n> "'
(Of course half that is just for prettification, if you don't mind ugly you can just say perl -ne 'print eval'.) It does pretty much everything you hope (provided your shell handles up arrow history for you). An example session:
./ip $a=2 2 > $a**10 1024 > $a *= 3 6 > sprintf '%lx', ~$a fffffff9 > use Digest::SHA 'sha256_hex' > sha256_hex 10 4a44dc15364204a80fe80e9039455cc1608281820fe2b24f1e5233ade6af1dd5 > exit
Although also very useful for testing small confusions about syntax, I've found it so handy as a sort of super calculator that I now keep an instance running all the time.

Replies are listed 'Best First'.
Re: Interactive perl
by itub (Priest) on Mar 23, 2005 at 15:01 UTC
    Have you tried perl -de1?
      That's just ugly. I use perl -wlne'eval;print$@if$@' (which doesn't print out the result of the eval; if I wanted that, I might do perl -wlne'print eval//$@//""').

        That's just ugly.

        Ugly? It's true that one doesn't automatically get a printout of the last expression evaluated (one has to ask for it with p or x or print, etc.), but getting readline/history, trace mode option, and the joy of x alone makes this well worth it, IMO.

        BTW, I'm not sure what you had intended with

        perl -wlne'print eval//$@//""'
        but it bombs:
        % perl -wlne 'print eval//$@//""' Warning: Use of "eval" without parentheses is ambiguous at -e line 1. Scalar found where operator expected at -e line 1, near "//$@" (Missing operator before $@?) syntax error at -e line 1, near "//$@" Search pattern not terminated at -e line 1.

        the lowliest monk

Re: Interactive perl
by chas (Priest) on Mar 24, 2005 at 03:51 UTC
    Another simple Perl "shell":
    while (<>) { if (/^!(.*)/) {eval system $1;} else {eval; print $@;} }

    (Could be made into a 1-liner, of course...this has system commands and error messages.)
    chas
      I have a longer script 'pash', that uses Term::ReadLine for history support. The only thing about 'pash' is that you hit enter twice - this allows multi-line entries.

      #!/usr/bin/perl # # pashn - simple perl shell with Readline support # $Id: pashn,v 1.1 2003/11/05 12:57:48 banjo Exp $ # # copyright 2003 Billy Naylor # banjo@actrix.co.nz # # This is GPL software... # See the GNU General Public Licence # http://www.fsf.org/licenses/gpl.txt # use Term::ReadLine; my $help = <<EOH; pashn is a simple perl shell, it allows you to type in raw perl, fragments and subs. You can also run shell commands by appending the command with a bang! However 'my' and 'local' will only work inside a single statement, and so multiline code can be entered you must hit return twice. But in return, Term::ReadLine is here to give us history and non-destructive backspaces, and multiline editing, and bash or vi keybindings, and of course you can easily 'use' oodles of CPAN modules, ? EOH my $terminal = Term::ReadLine->new('PASH'); $terminal->ornaments(0); $terminal->MinLine(undef); $| = 1; my $prompt = "pashn; "; while () { my $editbuffer = $terminal->readline( $prompt ); while () { my $b = $terminal->readline(''); last unless $b; $editbuffer .= "$b\n"; } chomp $editbuffer; $terminal->addhistory($editbuffer); if ($editbuffer =~ s/^\s*\!//) { print qx|$editbuffer|; next; } eval ("$editbuffer"); print $@ if $@; print "\n"; next; } sub help { print $help } sub quit { exit 0 } # thankyuoverymuchGoodnight exit 0;
Re: Interactive perl
by hossman (Prior) on Mar 23, 2005 at 23:41 UTC

    I'm fond of:   perl -ple'$_=eval'

Re: Interactive perl
by blazar (Canon) on Mar 25, 2005 at 13:24 UTC
    I think people prefer to use the debugger for this, e.g.:
    perl -de ''