How can I start an interactive console for Perl?

前端 未结 23 2333
故里飘歌
故里飘歌 2020-12-07 07:37

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

相关标签:
23条回答
  • 2020-12-07 07:44

    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.

    0 讨论(0)
  • 2020-12-07 07:45

    perl -d is your friend:

    % perl -de 0
    0 讨论(0)
  • 2020-12-07 07:46

    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
    
    0 讨论(0)
  • 2020-12-07 07:47

    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.

    0 讨论(0)
  • 2020-12-07 07:47

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

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

    0 讨论(0)
  • 2020-12-07 07:48

    There are two popular Perl REPLs.

    1. Devel::REPL is great.
    2. But IMO Reply is better.
    0 讨论(0)
提交回复
热议问题