in reply to Specific Indentation request for perl in Emacs

Instead of worrying too much about indentation while I'm coding, I just get it down and then use Perltidy to clean up the details.

You can use something like this to tidy up your Perl code from within Emacs:

(global-set-key "\C-xt" 'perltidy-region) (setq shell-command-default-error-buffer "perltidy-errors") (defun perltidy-region () "Run the perltidy parser on the current region." (interactive) (let ((start (mark)) (end (point)) (command "perltidy")) (shell-command-on-region start end command t t shell-command-d +efault-error-buffer)))

Put this in your ~/.emacs file, select the region you want to clean up and type C-xt or M-x perltidy-region.

Replies are listed 'Best First'.
Re^2: Specific Indentation request for perl in Emacs
by diotalevi (Canon) on Aug 07, 2004 at 03:14 UTC
    What, you use perltidy? Whenever I use it, I gain bugs! Is there some special magic to making it be nice?
      I use it constantly, on every single piece of code I write that's longer than a command-line. I've never had a single problem with it. Have you reported the bugs you've found?
        No, it was before I got into the swing of things with reporting bugs and such. I just don't trust the thing anymore especially since it is a perl parser.
      I don't use it often, but I've never had a problem with it personally. I mainly use it for downloaded code where the formatting was screwed up somehow. Sometimes, though, it has helped me to find a forgotten closing brace or some similar syntax mistake on my part.
      I read the warnings about Perltidy, but the only problem it gives me is formatting that I didn't ask for. I don't think it should be used on a large script that's not easy to test afterwards.

      All I use is:
      cd directory_of_PerlTidy
      perl perltidy -io -i=3 path_to_script_to _be_formatted

      I keep the above in a comment in the script it's used in. There should be a feature to automatically insert such a comment, with a time stamp of when it was titied.

        vroom wrote:
        cd directory_of_PerlTidy perl perltidy -io -i=3 path_to_script_to _be_formatt +ed
        I keep the above in a comment in the script it's used in. There should be a feature to automatically insert such a comment,
        If there's some boiler-plate that you want to appear in all of your code, you need an emacs templating system, such as template.el:
        http://emacs-template.sourceforge.net/
        I've been working on an emacs lisp package to use template.el with perl, which I call "perlnow.el":
        http://obsidianrook.com/perlnow
        Even if you don't want to use my code, you might like to look at the template.el templates there that I set-up to use for perl code.

        (Briefly: perlnow.el is designed to automate some routine tasks in perl programming: jumping into writing code, doing a "compile/check" on the code, jumping into writing tests for your code, running the tests, and so on. I've been finding it useful, but I'm still turning up minor bugs.)

        with a time stamp of when it was tidied.
        There I can't help you much off hand, you'll need to do some hackery of your own, either in perl or elisp.

        What I would probably do is write some elisp similar to what rhythmicus posted: it wouldn't be too hard to write an emacs extension that would do a perltidy on the current buffer, and insert or update a standard comment with a timestamp for when perltidy was run.

        20040810 Edit by ysth: move </i> out of <blockquote>