309

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

user2864740
  • 60,010
  • 15
  • 145
  • 220
ibz
  • 44,461
  • 24
  • 70
  • 86

24 Answers24

339

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

perl -de1

This command will start the Perl debugger (-d) on expression 1 (-e1), because the debugger wants something to debug (usually a file containing Perl code). The command could be written as perl -d -e 1 as well.

Alternatively there's Alexis Sukrieh's Perl Console application, but I haven't used it.

U. Windl
  • 3,480
  • 26
  • 54
Daniel Papasian
  • 16,145
  • 6
  • 29
  • 32
  • 38
    If you want something minimimaly usable consider to add [rlwrap](http://linux.die.net/man/1/rlwrap) `$ rlwrap perl -d -e 1` You would get history and a consistent prompt – albfan Oct 18 '14 at 08:18
  • `control`+`L` doesn't work under this command interface, how should I refresh the screen? – Zen Feb 26 '15 at 11:13
  • 2
    With the benefit of hindsight: [Ján Sáreník's answer](http://stackoverflow.com/a/22840242/45375) seems to provide the best almost-out-of-the-box solution (you may have to install `rlwrap`). All third-party REPL/console solutions, including `perlconsole`, are cumbersome to install and often have more limitations. – mklement0 Jul 08 '15 at 03:09
  • 4
    Unfortunately, `perl -de1` doesn't seem to support `my` variables, and hashes don't work either, for some reason… – Geremia Sep 05 '16 at 16:41
  • 2
    `perlconsole` doesn't suffer from these problems. – Geremia Sep 05 '16 at 16:53
68

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
  • 4,524
  • 1
  • 24
  • 24
  • I think it was assumed I didn't understand the question. I guess I'll edit it a bit. ;-) – Jon 'links in bio' Ericson Sep 16 '08 at 17:56
  • As of mid-2015: Simply running `[sudo] cpan Devel::REPL` didn't work for me, neither on OSX 10.10 (Perl 5.18.2) nor on Ubuntu 14.04 (Perl 5.18.2): I get runtime errors complaining about missing modules; installed one, then gave up after another missing module was reported. – mklement0 Jul 07 '15 at 16:31
  • @mklement0 There's a cpan setting to download and build prerequisite modules automatically. You might need to turn that setting on. Alternatively, the zero-config [cpanminus](https://github.com/miyagawa/cpanminus) application may be a better option. – Starfish Aug 07 '16 at 00:12
  • @Starfish: Thanks; to save future readers time (since it sounds like the answer may be short): how do you turn this setting on? – mklement0 Aug 07 '16 at 03:31
  • 1
    By the way, Matt Trout, the `re.pl` author, wrote an article listing several alternatives, and he himself suggests using `reply` instead of `re.pl`: http://shadow.cat/blog/matt-s-trout/mstpan-17/ – Denilson Sá Maia Oct 31 '16 at 10:16
  • Installed this on Centos 6 / Perl 5.10 just now - it dragged in 88 new/updated modules! Seems like a lot for a REPL ... – harmic Feb 01 '17 at 05:07
43

If you want history, use rlwrap. This could be your ~/bin/ips for example:

#!/bin/sh
echo 'This is Interactive Perl shell'
rlwrap -A -pgreen -S"perl> " perl -wnE'say eval()//$@'

And this is how it looks like:

$ ips
This is Interactive Perl shell
perl> 2**128
3.40282366920938e+38
perl> 
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
Ján Sáreník
  • 1,517
  • 1
  • 16
  • 13
  • 8
    Kudos for wrapping it up nicely - readline support is not only needed for history, but also for editing the command line. With the exception of multi-line support, this works well; here's an alias version as an alternative: `alias iperl='rlwrap -A -S "perl> " perl -wnE '\''say eval()//$@'\'`. OSX users can install `rlwrap` via [Homebrew](http://brew.sh) with `brew install rlwrap`. – mklement0 Jul 07 '15 at 16:26
  • 5
    Here is a version that gives a nice output when the expression evaluates to a list or a reference: `rlwrap -A -pgreen -S'perl> ' perl -MData::Dumper -wnE'say Dumper[eval()]//$@'` – kxmh42 Jul 14 '19 at 17:49
40

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
  • 21,344
  • 33
  • 76
  • 86
  • 5
    Does this work for one liners only or can handle multiline Perl code like conditional branching, loops, nested statements? – David Jan 23 '15 at 00:36
23

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
  • 445
  • 2
  • 4
  • 1
    Matt Trout's article has been down since some years ago, you can find it here: https://web.archive.org/web/20100212100032/http://chainsawblues.vox.com/library/post/a-perl-read-excute-print-loop-repl.html – lepe Jul 26 '16 at 06:40
  • 1
    Matt Trout now has a far more up to date list of REPLs he recommends: http://shadow.cat/blog/matt-s-trout/mstpan-17/ – Davor Cubranic Oct 06 '16 at 22:33
21

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 'links in bio' Ericson
  • 20,880
  • 12
  • 98
  • 148
20

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 Perl Console - Perl code interactive evaluator with completion.

Hope this helps!

Frank Wiles
  • 1,589
  • 11
  • 13
18

There are two popular Perl REPLs.

  1. Devel::REPL is great.
  2. But IMO Reply is better.

For reply just run it as a command. The module install the reply script. If you had installed the module and you don't have the command, check your PATH variable.

$ reply --help
reply [-lb] [-I dir] [-M mod] [--version] [--help] [--cfg file]
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
Eric Johnson
  • 17,502
  • 10
  • 52
  • 59
  • 9
    +1 for recommending Reply. Devel::REPL's author himself recommended Reply over its own solution when I ask about a good Perl REPL (console) on IRC. Reply also comes with auto-completion that is very handy for inspecting an object's available methods and attributes. I could install Reply on Ubuntu with the following command: `sudo apt-get install libreply-perl`. Then it can be run simply typing `reply` in a terminal. – Apteryx Jul 26 '15 at 19:37
  • 1
    How do you install amd run Reply? Neither the Metacpan, nor the Github page provides instructions. – Philippe Sep 17 '19 at 06:51
  • Check the edited answer. `reply` – Gilles Quénot Jan 03 '23 at 23:06
15

I've created perli, a Perl REPL that runs on Linux, macOS, and Windows.

Its focus is automatic result printing, convenient documentation lookups, and easy inspection of regular-expression matches.
You can see screenshots here.

It works stand-alone (has no dependencies other than Perl itself), but installation of rlwrap is strongly recommended so as to support command-line editing, persistent command history, and tab-completion - read more here.

Installation

  • If you happen to have Node.js installed:

    npm install -g perli
    
  • Otherwise:

    • Unix-like platforms: Download this script as perli to a folder in your system's path and make it executable with chmod +x.

    • Windows: Download the this script as perli.pl (note the .pl extension) to a folder in your system's path.
      If you don't mind invoking Perli as perli.pl, you're all set.
      Otherwise, create a batch file named perli.cmd in the same folder with the following content: @%~dpn.pl %*; this enables invocation as just perli.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 2
    I like it can launch `perldoc` from `perli` REPL like `'someCommand ?'` . Looks like it hasn't been updated for a while though, are you still maintaining perli? –  Jul 16 '17 at 09:05
  • 3
    @sdkks: If you find a problem, do [report it on GitHub](https://github.com/mklement0/perli/issues) and I'll try to fix it. `perli` hasn't been updated in a while, because in my personal use I haven't come across any issues, and few other people seem to use it, but, as far as I know, it still works as advertised. – mklement0 Jul 16 '17 at 11:57
  • 3
    I reported an issue and @mklement0 had it fixed and a new version released in less than 24 hours, FWIW. :) – David Moles Feb 23 '21 at 18:44
15

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

   perl -d -e 1
Clinton Pierce
  • 12,859
  • 15
  • 62
  • 90
9

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:
Archived "perlfaq3" page which contain question "Is there Perl Shell?"

For more information read perlfaq3 (current version).

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Michael Carman
  • 30,628
  • 10
  • 74
  • 122
7

perl -d is your friend:

% perl -de 0
Brian Phillips
  • 12,693
  • 3
  • 29
  • 26
7

re.pl from Devel::REPL

7

I always did:

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

With 5.10, I've switched to:

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

(rlwrap is optional)

ysth
  • 96,171
  • 6
  • 121
  • 214
6

You could look into psh here: http://gnp.github.io/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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
shelfoo
  • 1,569
  • 13
  • 15
5

Read-eval-print loop:

$ perl -e'while(<>){print eval,"\n"}'
KIM Taegyoon
  • 1,917
  • 21
  • 18
5

Update: I've since created a downloadable REPL - see my other answer.

With the benefit of hindsight:

  • The third-party solutions mentioned among the existing answers are either cumbersome to install and/or do not work without non-trivial, non-obvious additional steps - some solutions appear to be at least half-abandoned.
  • A usable REPL needs the readline library for command-line-editing keyboard support and history support - ensuring this is a trouble spot for many third-party solutions.
  • If you install CLI rlwrap, which provides readline support to any command, you can combine it with a simple Perl command to create a usable REPL, and thus make do without third-party REPL solutions.
    • On OSX, you can install rlwrap via Homebrew with brew install rlwrap.
    • Linux distros should offer rlwrap via their respective package managers; e.g., on Ubuntu, use sudo apt-get install rlwrap.
    • See Ján Sáreník's answer for said combination of rlwrap and a Perl command.

What you do NOT get with Ján's answer:

  • auto-completion
  • ability to enter multi-line statements

The only third-party solution that offers these (with non-trivial installation + additional, non-obvious steps), is psh, but:

  • it hasn't seen activity in around 2.5 years

  • its focus is different in that it aims to be a full-fledged shell replacement, and thus works like a traditional shell, which means that it doesn't automatically evaluate a command as a Perl statement, and requires an explicit output command such as print to print the result of an expression.


Ján Sáreník's answer can be improved in one way:

  • By default, it prints arrays/lists/hashtables as scalars, i.e., only prints their element count, whereas it would be handy to enumerate their elements instead.

If you install the Data::Printer module with [sudo] cpan Data::Printer as a one-time operation, you can load it into the REPL for use of the p() function, to which you can pass lists/arrays/hashtables for enumeration.

Here's an alias named iperl with readline and Data::Printer support, which can you put in your POSIX-like shell's initialization file (e.g., ~/.bashrc):

alias iperl='rlwrap -A -S "iperl> " perl -MData::Printer -wnE '\''BEGIN { say "# Use `p @<arrayOrList>` or `p %<hashTable>` to print arrays/lists/hashtables; e.g.: `p %ENV`"; } say eval()//$@'\'

E.g., you can then do the following to print all environment variables via hashtable %ENV:

$ iperl        # start the REPL
iperl> p %ENV  # print key-value pairs in hashtable %ENV

As with Ján's answer, the scalar result of an expression is automatically printed; e.g.:

iperl> 22 / 7  # automatically print scalar result of expression: 3.14285714285714
Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
4

Under Debian/Ubuntu:

$ sudo apt-get install libdevel-repl-perl
$ re.pl

$ sudo apt-get install libapp-repl-perl
$ iperl
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
4

Matt Trout's overview lists five choices, from perl -de 0 onwards, and he recommends Reply, if extensibility via plugins is important, or tinyrepl from Eval::WithLexicals, for a minimal, pure-perl solution that includes readline support and lexical persistence.

If you use the GNU Guix package manager, you can enter a working environment for tinyrepl with:

$ guix shell perl perl-eval-withlexicals perl-term-readline-gnu
[env]$ tinyrepl
re.pl$ 'hello world!'
"hello world!"
Apteryx
  • 5,822
  • 3
  • 16
  • 18
Davor Cubranic
  • 1,051
  • 10
  • 16
2

See also Stylish REPL (for GNU Emacs)

1

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

runrig
  • 6,486
  • 2
  • 27
  • 44
1

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

0

You can do it online (like many things in life) here:

https://www.tutorialspoint.com/execute_perl_online.php

Ross Attrill
  • 2,594
  • 1
  • 22
  • 31
0

You can use org-babel in emacs; Open an org-mode file, i.e., tmp.org, and then you can do:

#+begin_src perl :results output
@a = (1,5,9);
print ((join ", ", @a) . "\n");
$b = scalar @a;
print "$#a, $b\n";
print "$#a, " . @a . "\n";
print join ", ", 1..$#a; print "\n";
print join ", ", @a[0..$#a]
#+end_src

Pressing CTRL-c CTRL-c evals the block:

#+RESULTS:
#+begin_example
1, 5, 9
2, 3
2, 3
1, 2
1, 5, 9
#+end_example

I am not sure what emacs config this needs to work, but I think you can just install https://github.com/hlissner/doom-emacs and enable its perl and org-mode modules.

HappyFace
  • 3,439
  • 2
  • 24
  • 43