Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: how to indent/pretty print perl code

by clemburg (Curate)
on Nov 02, 2000 at 20:18 UTC ( [id://39665]=note: print w/replies, xml ) Need Help??


in reply to how to indent/pretty print perl code

Use emacs in batch mode.

With this emacs lisp code in file "perl-indent.el":

(cperl-mode) (indent-region 0 (buffer-size) nil) (save-buffer)

And this emacs command line call:

> emacs -batch try.pl -l .emacs -l perl-indent.el

This file "try.pl":

#!/usr/bin/perl -w use strict; for (1) { print "emacs rules"; } while (1) { print " ... at least in viper mode\n"; }

turns into:

#!/usr/bin/perl -w use strict; for (1) { print "emacs rules"; } while (1) { print " ... at least in viper mode\n"; }

The call to the ".emacs" initialization file is necessary since emacs won't load it per default in batch mode, and you want your personal indentation preferences to be used, of course.

Christian Lemburg
Brainbench MVP for Perl
http://www.brainbench.com

Replies are listed 'Best First'.
RE: Re: how to indent/pretty print perl code
by ZZamboni (Curate) on Nov 02, 2000 at 21:20 UTC
    This is very cool, clemburg. ++ for that. I was going to comment about cperl-mode, but I didn't know how to automate the process. Thanks!

    Just for the sake of completeness, be aware that cperl sometimes breaks with funky perl code such as regexes, here documents, unusual quoting and things like that. But other than that it's an excellent way of doing it.

    For printing Perl code, I usually use xemacs' "pretty-print" option, which is loaded with some package I can't remember right now. I don't know how to invoke it in batch mode though - you have to manually load the file and select the option from the menu. Pretty-print generates postscript according to your current font-lock settings, so you get colors (shading in a b/w printer), bold, etc.

    --ZZamboni

      Yup, you are right about cperl mode. It's just that I don't know a better replacement. In my experience, nearly all modes for Perl break on complicated constructs (this is BTW a good incentive to keep your Perl readable).

      If you can find out where this "pretty-print" option comes from, it should not be hard to automate it, too - I will be glad to help (though I don't use xemacs, but GNU emacs, and pretty-print is not there in my emacs :-( ... might be the incentive to go for xemacs?).

      Christian Lemburg
      Brainbench MVP for Perl
      http://www.brainbench.com

        I think the package is ps-print.el (I'm from a remote terminal right now, so I can't check thoroughly), and it says it belongs to GNU Emacs, so I would assume it is present there too. When I read the file I assumed it would only work from interactive mode because it says it needs a font-lock package. But if you figure out how to make it work from batch mode, please let me know :-)

        --ZZamboni

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://39665]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 06:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found