Update: I've since created a downloadable REPL - see my other answer.
With the benefit of hindsight:
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.
rlwrap
via Homebrew with brew install rlwrap
.rlwrap
via their respective package managers; e.g., on Ubuntu, use sudo apt-get install rlwrap
.rlwrap
and a Perl command.What you do NOT get with Ján's answer:
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:
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