Emacs - Support for Perl embedded in GNU Emacs


    perlmacs -w -MEmacs -e main -- --display :0.0 file.txt
    #! /usr/bin/perlmacs
    use Emacs;
    use Emacs::Lisp;
    setq { $mail_self_blind = t; };
    exit main ($0, "-q", @ARGV);


This module replaces STDIN, STDOUT, STDERR, %ENV, and %SIG with versions that work safely within an Emacs session. It also defines a function named main, which launches an Emacs editing session from within a script.

main (CMDLINE)

When you use Emacs in a perlmacs script, a Perl sub named main may be used to invoke the Emacs editor. This makes it possible to put customization code, which would normally appear as Lisp in ~/.emacs, into a Perl script. For example, this startup code

     user-mail-address "gnaeus@perl.moc"
     mail-self-blind t
     mail-yank-prefix "> "
    (put 'eval-expression 'disabled nil)
    (global-font-lock-mode 1 t)
    (set-face-background 'highlight "maroon")
    (set-face-background 'region "Sienna")

could be placed in a file with the following contents:

    #! /usr/local/bin/perlmacs
    use Emacs;
    use Emacs::Lisp;
    setq {
        $user_mail_address = 'gnaeus@perl.moc';
        $mail_self_blind = t;
        $mail_yank_prefix = '> ';
        $eval_expression{\*disabled} = undef;
    &global_font_lock_mode(1, t);
    &set_face_background(\*highlight, "maroon");
    &set_face_background(\*region, "Sienna");
    exit main($0, "-q", @ARGV);

When you wanted to run Emacs, you would invoke this program.

The arguments to main correspond to the argv of the main function in a C program. The first argument should be the program's invocation name, as in this example. -q inhibits running ~/.emacs (which is the point, after all).


Reading a line from Perl's STDIN filehandle causes a string to be read from the minibuffer with the prompt "Enter input: ". To show a different prompt, use:

    use Emacs::Lisp;
    $string = &read_string ("Prompt: ");


Printing to Perl's STDOUT filehandle inserts text into the current buffer as though typed, unless you have changed the Lisp variable standard-output to do something different.

STDERR and `warn'

Perl's warn operator and STDERR filehandle are redirected to the minibuffer.


Access to %ENV is redirected to the Lisp variable process-environment.


Setting signal handlers is not currently permitted under Emacs.



Copyright (C) 1998,1999,2000 by John Tobey, All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  MA 02111-1307  USA


perl, Emacs::Lisp, emacs.