---
title: "emacs中格式化perl代码"
author: "jedy"
date: "2005-12-01T07:52:00Z"
tags: []
slug: "emacs-perl"
draft: false
lastmod: "2005-12-01T07:52:00Z"
id: 304770
url: "https://jedy.iteye.com/blog/304770"
---

首先需要[perltidy](http://perltidy.sourceforge.net/)

```

#!perl.exe
# perltidy.pl
use Tidy;
Tidy::perltidy();

将下面这个函数加到emacs中
调用此函数，如果选择了region，就格式化region，否则格式化全文。

;; emacs
(defun wh-perl-format ()
  (interactive)
  (message "Formatting, please wait...")
  (shell-command-on-region 
   (if mark-active (region-beginning) (point-min))
   (if mark-active  (region-end)(point-max))
   (concat "perl c:/perltemp/finished/perltidy.pl -i=4 -nt -ci=2 -ce -pt=2 -l=100 -st ")
   (current-buffer)
   t
   "*Messages*"
   nil
   )
  )
```