Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

how to indent/pretty print perl code

by snowcrash (Friar)
on Nov 02, 2000 at 16:23 UTC ( #39624=perlquestion: print w/replies, xml ) Need Help??
snowcrash has asked for the wisdom of the Perl Monks concerning the following question:

hi! i'm looking for a tool like gnu indent the beautifies my perl code. i work together with other programmers and all have different styles and use different editors. gnu indent is a great tool but doesn't recognize perl syntax, and even destroys my sources.

i found a reference to some tool named vgrind in the faq. after some research i found several versions of the source code, but didn't get them to compile || got segfaults when i managed to compile them.

how do you handle that problem? it would be nice if there was a tool that can be used automated together with cvs...

yt, snowcrash

Replies are listed 'Best First'.
Re: how to indent/pretty print perl code
by clemburg (Curate) on Nov 02, 2000 at 20:18 UTC

    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

      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

Re: how to indent/pretty print perl code
by autark (Friar) on Nov 02, 2000 at 17:08 UTC
    The module 'B::Deparse' might help you out:
    perl -MO=Deparse
    However, it may fail from time to time...


Re: how to indent/pretty print perl code
by clemburg (Curate) on Nov 03, 2000 at 21:11 UTC

    On ZZamboni's request, this is the emacs code you will need to print a postscript copy of your pretty-printed perl code (in file "perl-indent-ps.el"):

    (cperl-mode) (indent-region 0 (buffer-size) nil) (save-buffer) (ps-print-buffer (cond (command-line-args-left (car command-line-args-left)) (t nil)))

    (Sorry about the peculiar indentation, fellow LISPers, but the formatter here makes it necessary to do something like this.)

    You have to call emacs like this to make it work:

    > emacs -batch -l .emacs -l perl-indent-ps.el

    If you omit the last argument in the preceding command line, your postscript will be send to the printer (at least this is what my emacs docs say, I could not test this since there is no printer hooked up to the server I currently work on).

    Christian Lemburg
    Brainbench MVP for Perl

Re: how to indent/pretty print perl code
by duct_tape (Hermit) on Feb 05, 2003 at 23:23 UTC
    I know this node is a bit old, but I just found a tool that was worked fairly well. I used it to fix some terrible formatting that was done in a project that I inherited at work.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://39624]
Approved by root
[Discipulus]: yes I know, bro' and I also know I cannot compete. But no one ask for oneliners.. ;=)
[Discipulus]: and i'm just happy for rising the black camel in the main ladderboard

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2017-10-22 20:18 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (275 votes). Check out past polls.