Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a prettyprint tool for Perl? I am looking for a tool which will take my existing Perl scripts and indent them properly.

I tried searching CPAN and google using the term prettyprint and did not find what I was looking for.

Replies are listed 'Best First'.
RE: Prettyprint
by Adam (Vicar) on Sep 08, 2000 at 21:59 UTC
    The deparser, when it works, returns your code (more or less) in a pretty format.
    perl -MO=Deparse MyScript.pl
Re: Prettyprint
by mdillon (Priest) on Sep 08, 2000 at 21:48 UTC
Re: Prettyprint
by merlyn (Sage) on Sep 08, 2000 at 22:47 UTC
RE: Prettyprint
by geektron (Curate) on Sep 08, 2000 at 23:20 UTC
    there's actually an example in Damian Conway's OO-Perl for pretty printing. it's in the Operator Overloading section, IIRC.
Re: Prettyprint
by Anonymous Monk on Sep 08, 2000 at 23:39 UTC
    Thanks everyone. Deparse works except for one odd feature. If I define a subroutine sub mysub{} at the bottom of my file then the Deparser changes the

    mysub(myparam)

    calls to

    mysub myparam

    and the calls to mysub then break. If I put the parentheses back in then the prettified code works great.
      what about "
      perl -MO=Deparse,-p yourscript.pl
      "? this should put a _lot_ of parentheses...
      That's somewhat odd. What does mysub{} do?
      Deparse is actually returning the interpreter's version of your code to you, so it will have some amount of inlining performed and a few other things specific to the state of your code when you deparsed it. In other words, your code will be slightly optimized and you should beware of early optimization. Perhaps I should have phrased my earlier statement:

      The deparser, when it works, returns (more or less) your code in a pretty format.

        mysub{} prints HTML code. Just one of life's mysteries, I guess.

        I read that in Perl the parentheses surrounding a subroutine's arguments are completely optional, so it should not matter whether I use parentheses to call subroutines in my prettified code, but it does.

      Sounds like Deparse could use a patch, though I don't think it ever was intended to be used as a pretty printer. I bet you'll run into other output from Deparse that isn't working code as well.

              - tye (but my friends call me "Tye")
A reply falls below the community's threshold of quality. You may see it by logging in.