Reputation: 132
I'm learning how to use Perl in Emacs. I used to run R with R-Studio.
How do I execute a command without leaving Emacs?
Example: In R-studio I type
print("hello world")
and press Ctrl+Enter and R-studio executes the command and prints "hello world"
.
How do I do the same in Emacs for a Perl command?
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.
Upvotes: 4
Views: 4121
Reputation: 63
I use
Meta-x compile.
Meta is the alt key. The buffer will open a command to make -k the first time. You can just erase that and type
perl file.pl
Run that command the first time, and emacs will remember it for the next time. If you want to get really fancy, you can open your ~/.emacs.d/init.el file and put in the following
(add-hook 'cperl-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
(format "perl %s" (file-name-nondirectory buffer-file-name)))))
(add-hook 'perl-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
(format "perl %s" (file-name-nondirectory buffer-file-name)))))
(global-set-key (kbd "<f5>") 'compile)
Now to compile, you can just hit f5 and emacs should run the correct command. Emacs will save this command for the next time you compile as well. I prefer cperl-mode, but this should work with perl-mode as well. I got this from a guy doing the same thing but for javascript here
can I change emacs' default compile command?
You can also run
Meta-x shell
and use the shell window that opens to run
perl file.pl
Often I dont bother doing either of those. I will open emacs with an ampersand to return the command line like this
emacs file.pl &
If you are running a GUI, the terminal will open that file in Emacs and return the command line. When I want to run it, I just go back to the terminal window and run
perl file.pl
If I am not running a GUI, like via ssh or wsl, I will usually just open two terminal windows. One for emacs, and one for running commands.
Upvotes: 0
Reputation: 20248
For all kinds of interpreted languages, I use isend-mode, which allows sending parts of a buffer to a terminal in another buffer.
Here is how you would use it (after having installed it):
Open an ansi-term
buffer:
M-xansi-term
RET/bin/bash
RET
and run an interactive perl session inside:
perl -d -e 42
RET
Alternatively, install term-run.el and directly launch the interactive perl session in a term
buffer:
M-xterm-run-shell-command
RETperl -d -e 42
RET
Open the buffer with the code you want to execute, and associate it to the interpreter buffer:
M-xisend
RET*ansi-term*
RET
Hit C-RET in the perl buffer to send the current line to the interpreter in the ansi-term
buffer. If a region is active, all lines spanning the region will be sent.
Below is a proposed setup allowing to better take advantage of the perl debugger specific commands. With the following customization, x
is prepended to all instructions (so that you see the results), except 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)
Upvotes: 3
Reputation: 118
If I'm running Perl from within Emacs, it's typically to process a region of text or to insert the results of a command into the buffer.
M-!
(that's Meta-Shift-1) will execute a shell command, and display the results in the mini buffer.
Prefixing the command with a prefix argument (M-1) will insert the command's output at point.
For example, stubbing out an (x)HTML file into an empty buffer:
M-1 M-!
perl -MCGI=:standard -e 'print start_html("Hello World"),end_html'
will produce the following:
<!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>
Upvotes: 1
Reputation: 2393
If you can load CPerl mode (Emacs should do that by default if you load a file with the .pl extension), you should also get the Perl menu in your menubar (assuming you're running in a GUI, not a simple terminal).
From the Perl menu, just choose "Run"! The current buffer (Perl script) will be run (you'll be prompted for any arguments first), and the results displayed in a new buffer.
Upvotes: 3