Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: how to indent/pretty print perl code

by clemburg (Curate)
on Nov 02, 2000 at 20:18 UTC ( #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 -l .emacs -l perl-indent.el

This file "":

#!/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

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.


      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

        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 :-)


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://39665]
and not a whimper to be heard...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2017-10-21 02:28 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (269 votes). Check out past polls.