9.6. Perl Support


Emacs has Perl support. Indeed, much like Perl itself, there are multiple ways to get things done in this case, multiple Perl modes: the classic Perl mode (which comes up by default) and the more popular CPerl mode.

You should have a version of CPerl mode built right in, but you can also pick up the latest release from CPAN (the Comprehensive Perl Archive Network) online at http://www.cpan.org.

You can add one of the following pairs of lines to your .emacs file to make sure CPerl mode is invoked rather than Perl mode

;; load cperl-mode for perl files (fset 'perl-mode 'cperl-mode) ;; or maybe use an alias (defalias 'perl-mode 'cperl-mode)

CPerl mode is mostly like cc-mode with respect to motion and other programming language features. It also includes fun debug operations. You can start the debugger with M-x cperl-db. You'll be prompted to verify the debugger command and then be dropped into a split-screen mode. One buffer allows you to drive the normal perldb environment with all the regular commands you're accustomed to using in the Perl debugger.

The other buffer shows your script and follows along as you work through the debugger. It tracks the line you're about to execute as you issue commands in the other buffer. It's amazing how quickly you grow to depend on having such tools available while you're developing scripts. It is worth trying out if you've never done it before.

9.6.1 Perl Caveats

A big reason we wanted to mention Perl mode here is to highlight a few caveats. Perl is an amazingly expressive language much more akin to the idioms found in human languages than just about any other computer language out there. That expressiveness can cause problems especially when considering the expressiveness of regular expressions.

Perl supports all sorts of "funny" variable names like $' and $/. CPerl mode boasts the use of a syntax table to help understand most of Perl's odd and occasionally disruptive verbiage. The older Perl mode has no such trick up its sleeves and suffers under many circumstances in the font-lock and indentation realms. This is one of the main reasons to make the leap into CPerl mode.

Even with that syntax table, though, you'll probably find some combinations of variables and strings that give Emacs headaches. Sometimes restructuring your code will help, sometimes not. The important thing to remember is that it won't harm your program at all. It might make things a bit less readable, but the script itself should run just fine. And if it doesn't, you can always launch the debugger to find out why!

Here are some parting .emacs thoughts for you Perl programmers. These lines select cperl-mode as the default and make sure the syntax highlighting is turned on. These lines also turn on folding (outline-minor-mode in the snippet below). Folding allows you to "hide" chunks of your code, such as functions where the body of the function is "folded" into the name. That can make it easier to get a grip on everything that is going on in the file. Try it it can become addictive!

;; Turn on highlighting globally (global-font-lock-mode t) ;; automatically load cperl-mode for perl files (fset 'perl-mode 'cperl-mode) ;; show only the toplevel nodes when loading a file (add-hook 'cperl-mode-hook 'hide-body) ;; outline minor mode with cperl (add-hook 'cperl-mode-hook 'outline-minor-mode) ;; Change the prefix for outline commands from C-c @ to C-c C-o (setq outline-minor-mode-prefix "\C-co") (load-file "cperl-mode.el")



Learning GNU Emacs
Learning GNU Emacs, Third Edition
ISBN: 0596006489
EAN: 2147483647
Year: 2003
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net