8

Python offers an interactive interpreter allowing the evaluation of little code snippets by submitting a couple of lines of code to the console. I was wondering if a tool with similar functionality (e.g. including a history accessible with the arrow keys) also exists for Perl?

There seem to be all kinds of solutions out there, but I can't seem to find any good recommendations. I.e. lots of tools are mentioned, but I'm interested in which tools people actually use and why. So, do you have any good recommendations, excluding the standard perl debugging (perl -d -e 1)?

Here are some interesting pages I've had a look at:

Community
  • 1
  • 1
dlaehnemann
  • 671
  • 5
  • 17

4 Answers4

7
   perl -d -e 1

Is perfectly suitable, I've been using it for years and years. But if you just can't, then you can check out Devel::REPL

Len Jaffe
  • 3,442
  • 1
  • 21
  • 28
6

If your problem with perl -d -e 1 is that it lacks command line history, then you should install Term::ReadLine::Perl which the debugger will use when installed.

tjd
  • 4,064
  • 1
  • 24
  • 34
  • Nice catch btw. I always install that to amke debugging better, so I didn't think to explain it. I'm glad that you did. – Len Jaffe Sep 18 '13 at 15:17
4

Even though this question has plenty of answers, I'll add my two cents on the topic. My approach to the problem is easy if you are a ViM user, but I guess it can be done from other editors as well:

  1. Open your ViM, and type your code. You don't need to save it on any file.

  2. :w !perl for evaluation (:w !COMMAND pipes the buffer to the process obtained by running COMMAND. In this case the mighty perl interpreter!)

  3. Take a look at the output

This approach is good for any interpreted language, not just for Perl.

In the case of Perl it is extremely convenient when you are writing your own modules, since in my experience the perl interpreter will refuse to reload a module (even when loading was attempted and failed). On the minus side, you will loose all your context every time, so if you are doing some heavy or slow operation, you need to save some intermediate results (whilst the perl console approach preserves the previously computed data).

If you just need the evaluation of an expression - which is the other use case for a perl console program - another good alternative is seeing the evaluation out of a perl -e command. It's fast to launch, but you have to deal with escaping (for this thing the $'...' syntax of Bash does the job pretty well.

Dacav
  • 13,590
  • 11
  • 60
  • 87
  • This is awesome! I've been using vim as my editor for more than a year now, and I never really used the `:w !COMMAND` feature before. Now that I'm having to start using perl + the lack of an IDE or interactive mode (with command history + keeping track of variables), this vim feature will help me immeasurably. Thank you! – Reilstein Sep 08 '16 at 18:40
  • 1
    @Reilstein, Yes, a Hacker’s strength flows from the Unix. But beware of the IDEs. Auto-generated code, copy-and-paste, lack of understanding. The dark side of the Source are they. – Dacav Sep 08 '16 at 20:27
0

Just use to get history and arrows:

rlwrap perl -de1
aod
  • 155
  • 1
  • 5