views:

1932

answers:

15

How can I start an interactive console for Perl, similar to the irb command for Ruby or python for Python?

+2  A: 

perl -d is your friend:

% perl -de 0
Brian Phillips
+3  A: 

You can always just drop into the built-in debugger and run commands from there.

   perl -d -e 1
clintp
A: 

There isn't an interactive console for Perl built in like Python does. You can however use the Perl Debugger to do debugging related things. You turn it on with the -d option, but you might want to check out 'man perldebug' to learn about it.

After a bit of googling, there is a separate project that implements a Perl console which you can find at http://www.sukria.net/perlconsole.html.

Hope this helps!

Frank Wiles Revolution Systems www.revsys.com

Frank Wiles
+5  A: 

You can use the perl debugger on a trivial program, like so:

perl -d -e 1

Alternatively there's software to be more of a console, but I haven't used it: http://www.sukria.net/perlconsole.html

Daniel Papasian
I wasn't aware of the perlconsole you linked to - looks nice!
Brian Phillips
+1  A: 

I use the command line as a console:

$ perl -e 'print "JAPH\n"'

Then I can use my bash history to get back old commands. This does not preserve state, however.

This form is most useful when you want to test "one little thing" (like when answering Perl questions). Often, I find these commands get scraped verbatim into a shell script or makefile.

Jon Ericson
+1  A: 

You could look into psh here: http://www.focusresearch.com/gregor/sw/psh/

It's a full on shell (you can use it in replacement of bash for example), but uses perl syntax.. so you can create methods on the fly etc.

shelfoo
A: 

Perl doesn't have a console but the debugger can be used as one. At a command prompt, type perl -de 1. (The value "1" doesn't matter, it's just a valid statement that does nothing.)

There are also a couple of options for a Perl shell.

For more information read perlfaq3.

Michael Carman
A: 

Also look for ptkdb on CPAN: http://search.cpan.org/search?query=ptkdb&mode=all

runrig
+8  A: 

I think you're asking about a REPL (Read, Evaluate, Print, Loop) interface to perl. There are a few ways to do this:

  • Matt Trout has an article that describes how to write one
  • Adriano Ferreira has described some options
  • and finally, you can hop on IRC at irc.perl.org and try out one of the eval bots in many of the popular channels. They will evaluate chunks of perl that you pass to them.
amoore
+11  A: 

Not only did Matt Trout write an article about a REPL, he actually wrote one - Devel::REPL

I've used it a bit and it works fairly well, and it's under active development.

BTW, I have no idea why someone modded down the person who mentioned using "perl -e" from the console. This isn't really a REPL, true, but it's fantastically useful, and I use it all the time.

Dave Rolsky
I think it was assumed I didn't understand the question. I guess I'll edit it a bit. ;-)
Jon Ericson
+2  A: 

I wrote a script I call "psh":

#! /usr/bin/perl

while (<>) {
  chomp;
  my $result = eval;
  print "$_ = $result\n";
}

Whatever you type in, it evaluates in Perl:

> gmtime(2**30)
gmtime(2**30) = Sat Jan 10 13:37:04 2004

> $x = 'foo'
$x = 'foo' = foo

> $x =~ s/o/a/g
$x =~ s/o/a/g = 2

> $x
$x = faa
raldi
+1  A: 

re.pl from Devel::REPL

A: 

Sepia and PDE have also own REPLs (for GNU Emacs).

A: 

See also Stylish REPL (for GNU Emacs) http://blog.jrock.us/articles/Stylish%20REPL.pod

A: 

I always did:

perl -wlne'eval;print$@if$@'

With 5.10, I've switched to:

perl -wnE'say eval()//$@'
ysth