[perl] How can I start an interactive console for Perl?

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

This question is related to perl console interactive read-eval-print-loop

The answer is


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!


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.

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



perl -d is your friend:

% perl -de 0

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

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.


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


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.


Read-eval-print loop:

$ perl -e'while(<>){print eval,"\n"}'

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.

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.


There are two popular Perl REPLs.

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

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.


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

   perl -d -e 1

re.pl from Devel::REPL


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.


Under Debian/Ubuntu:

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

$ sudo apt-get install libapp-repl-perl
$ iperl

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.


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.



perl -d is your friend:

% perl -de 0

Read-eval-print loop:

$ perl -e'while(<>){print eval,"\n"}'

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!


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.


re.pl from Devel::REPL


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

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


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.


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.


Under Debian/Ubuntu:

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

$ sudo apt-get install libapp-repl-perl
$ iperl

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

   perl -d -e 1

re.pl from Devel::REPL


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


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.

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.


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


re.pl from Devel::REPL


perl -d is your friend:

% perl -de 0

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.


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

   perl -d -e 1

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.

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!


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

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


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.


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

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


I always did:

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

With 5.10, I've switched to:

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

(rlwrap is optional)


perl -d is your friend:

% perl -de 0

I always did:

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

With 5.10, I've switched to:

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

(rlwrap is optional)


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.


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


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.



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.


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.


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


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.


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

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> 

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

I always did:

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

With 5.10, I've switched to:

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

(rlwrap is optional)


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.


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

There are two popular Perl REPLs.

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

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


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

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

   perl -d -e 1

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.


Examples related to perl

The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing while starting Apache server on my computer "End of script output before headers" error in Apache Perl - Multiple condition if statement without duplicating code? How to decrypt hash stored by bcrypt Split a string into array in Perl Turning multiple lines into one comma separated line String compare in Perl with "eq" vs "==" how to remove the first two columns in a file using shell (awk, sed, whatever) Find everything between two XML tags with RegEx Difference between \w and \b regular expression meta characters

Examples related to console

Error in MySQL when setting default value for DATE or DATETIME Where can I read the Console output in Visual Studio 2015 Chrome - ERR_CACHE_MISS Swift: print() vs println() vs NSLog() Datatables: Cannot read property 'mData' of undefined How do I write to the console from a Laravel Controller? Cannot read property 'push' of undefined when combining arrays Very simple log4j2 XML configuration file using Console and File appender Console.log not working at all Chrome: console.log, console.debug are not working

Examples related to interactive

Interactive shell using Docker Compose Passing arguments to an interactive program non-interactively How to convert string to integer in UNIX How can I start an interactive console for Perl?

Examples related to read-eval-print-loop

How do I load a file into the python console? How to save a Python interactive session? How can I start an interactive console for Perl?