Running Perl in Emacs, the Easiest

I am learning how to use Perl in Emacs. I used to use R with R-Studio.

How to execute a command without leaving Emacs?

Example: In R-studio, I print

print("hello world") 

and press Ctrl + Enter , and R-studio will execute the command and print "hello world" . How to do the same in Emacs for the Perl team?

I usually type Ctrl + X Ctrl + F test.pl

  print "hello world"; 

and then I donโ€™t know what to do for Emacs to execute the command.

+4
source share
4 answers

For all kinds of interpreted languages, I use isend-mode , which allows you to send portions of the buffer to the terminal in another buffer.

Here is how you could use it (after installing it):

  • Open the ansi-term buffer:

    Mx ansi-term RET /bin/bash RET

    and start an interactive perl session inside:

    perl -d -e 42 RET

    Alternatively, set term-run.el and immediately start an interactive perl session in the term buffer:

    Mx term-run-shell-command RET perl -d -e 42 RET

  • Open the buffer with the code you want to execute and bind it to the interpreter buffer:

    Mx isend RET *ansi-term* RET

  • Press C-RET in the perl buffer to send the current line to the interpreter in the ansi-term buffer. If the region is active, all lines covering the region will be sent.


The following is a suggested setting to better use the special perl debugger commands. The next time you configure x added to all instructions (so you can see the results), except for print commands.

 (defun isend--perl (buf-name) "Prepend 'x ' to normal perl instructions. Leave 'print' instructions untouched." (with-current-buffer buf-name (goto-char (point-min)) (unless (looking-at "[[:space:]]*print") (insert "x "))) (insert-buffer-substring buf-name)) (defun isend-default-perl-setup () (when (eq major-mode 'perl-mode) (set (make-local-variable 'isend-send-line-function) #'isend--perl))) (add-hook 'isend-mode-hook #'isend-default-perl-setup) 
+4
source

This is what I use:

Meta -x shell

perl test.pl

+1
source

If you can load CPerl mode (Emacs should do this by default if you download a file with the .pl extension), you should also get the Perl menu in your menu bar (assuming that you are working in a graphical interface and not in a simple terminal )

From the Perl menu, select "Run"! The current buffer (Perl script) will be launched (first you will be offered all the arguments), and the results are displayed in a new buffer.

+1
source

If I run Perl from Emacs, it usually processes the text area or inserts the results of the command into the buffer.

M-! (which is Meta-Shift-1) will execute a shell command and display the results in a mini-buffer.

A command prefix with a prefix argument (M-1) will insert the command output at a point.

For example, cutting (x) an HTML file into an empty buffer: M-1 M-! perl -MCGI=:standard -e 'print start_html("Hello World"),end_html'

The following will be displayed:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"> <head> <title>Hello World</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body> 
0
source

All Articles