Sailor Family


Sailor At Work

Rubik Cube


Web Tools

Drop me a note
Email Web Sailor
Welcome to Web Sailor's Home
Home > Emacs/Cygwin > Using Emacs for Perl Programming

Using Emacs for Perl Programming

There is a default perl-mode for Emacs. However, there is a better one hidden within Emacs. The following lisp statements will activate cperl-mode.

;; Use cperl-mode instead of the default perl-mode
(add-to-list 'auto-mode-alist '("\\.\\([pP][Llm]\\|al\\)\\'" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl5" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("miniperl" . cperl-mode))

Since I do not like the default indentations, I have the followings:

(add-hook 'cperl-mode-hook 'n-cperl-mode-hook t)
(defun n-cperl-mode-hook ()
  (setq cperl-indent-level 4)
  (setq cperl-continued-statement-offset 0)
  (setq cperl-extra-newline-before-brace t)
  (set-face-background 'cperl-array-face "wheat")
  (set-face-background 'cperl-hash-face "wheat")

Using Emacs for Perl Scripts

If you have perl installed on your computer, you may debug a perl script by simply typing "alt-x perldb" "perl -d". Once the command is entered, you will see 2 buffers in Emacs, the top buffer is the debugger, the bottom buffer is your perl script.

ActiveState Perl / Cygwin Bash Shell / Emacs

If you have ever tried to use ActiveState Perl in a Cygwin Bash shell, you will find that only the commands of the following formats can be run successfully.

  • perl
  • perl c:/workarea/
  • c:/perl/bin/perl

On the other hand, the following formats will give you a problem complaining the perl script specified cannot be found.

  • perl -S
  • perl /cygdrive/c/workarea/
  • c:/perl/bin/perl -S

This is because of the fact that ActiveState Perl program cannot understand Cygwin's mount drives.

There is a solution by Mark Parris to allow users to run ActiveState Perl in Cygwin Bash shell.

My solution is to make a C (perlproxy.c) executable that will convert the Cygwin path to Windows path before invoking the ActiveState Perl. You may have to change the path of ActiveState perl.exe in perlproxy.c according to your installation.

  • gcc -mno-cygwin perlproxy.c -o perl.exe
  • mv perl.exe /usr/local/bin/perl.exe

Now your Perl scripts should have #!/usr/local/bin/perl as the first line for things to work properly. The only side effect that I have seen so far is that you cannot type /usr/local/bin/perl in a shell to run the Perl program interactively. You will have to specify the actual path of the ActiveState Perl (for example, c:/programs/perl/bin/perl) in this case.

Langkawi Sunset