Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

perltidy IF continuation indentation

by jaa (Friar)
on Oct 04, 2005 at 08:34 UTC ( #497155=perlquestion: print w/replies, xml ) Need Help??

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


I am looking for a perltidy option that does this:

return $ship->chargeWeapons( "phaser-canon" ) if $encounter->description eq 'klingon' and $ship->firepower >= $encounter->firepower and $location->status ne 'neutral';
instead of this...
return $ship->chargeWeapons( "phaser-canon" ) if $encounter->description eq 'klingon' and $ship->firepower >= $encounter->firepower and $location->status ne 'neutral';
Note that I want the wrapped ANDs to be indented, rather than at the same indentation level as the IF.

A thousand apologies if this is obvious - I have googled and manned to no avail.

Regards, & thanks


Replies are listed 'Best First'.
Re: perltidy IF continuation indentation
by jaa (Friar) on Oct 06, 2005 at 14:49 UTC
    Update: I mailed Steve, the maintainer of perltidy a few days ago. He responded immediately - (thanks Steve!) and said he would look into it...

    He initially thought the best approach would be to add recognition of trailing if, unless constructs and appropriate indentation,... but after further investigation thinks that this is too complex to do.

    Steve's suggestion is to use brackets to create a container and have -i=4 -ci=2, which is exactly what sauoq has suggested. I think I will go with this mod to PBP - it breaks the recommendation to remove redundent brackets, and changes the appearance of continuation indented code, but seems to better achieve the goal of clear code layout.

    Regards & thanks,


Re: perltidy IF continuation indentation
by blazar (Canon) on Oct 04, 2005 at 08:45 UTC
    There may well be an appropriate answer to your question, and I hope so, but due to the notorious fact nothing but perl can parse Perl, it may be difficult.

    I'm bothered too that my own editor, JED, which generally does a very good job of indenting the code for me as I type it, occasionally doesn't -- for example by not telling correctly anonymous hashrefs from code blocks (but then even perl itself needs some help sometimes in this regards).

Re: perltidy IF continuation indentation
by sauoq (Abbot) on Oct 04, 2005 at 14:05 UTC

    Well, I don't know what switches if any will give that behavior, but if you simply put parens around your clause, it'll indent nicely by default.

    "My two cents aren't worth a dime.";
      Interesting observation!

      I have set my perltidy options to the ones in Damian Conway's 'Perl Best Practices', and with these, adding the brackets makes no difference.

      A bit of experimentation shows that this is because I have -i=2 and -ci=2. I have both the indent and continuation-indent settings set to the same value.

      Without brackets, perltidy simple inserts a series of continuation-indents.

      Add the brackets, and perltidy inserts a continuation-indent in front of the IF and then, recognising the presence of a container (the brackets) adds full indents as it wraps the container content.

      The observation is much appreciated, as it has lead me to a better understanding of perltidy, but sadly doesn't really help. :(

      Is anyone else going to use the PBP style? Do you just live with the ugliness of continuation-indent IFs?



        Is anyone else going to use the PBP style?

        Not if it makes my code look like that! ;-)

        I might be able to live with continuation-indent set to 1 instead of 2 though. As shown below, the perltidy documentation suggests that it should be no greater than that if -i=2.

        When default values are not used, it is suggested that the value n given with -ci=n be no more than about one-half of the number of spaces assigned to a full indentation level on the -i=n command.

        "My two cents aren't worth a dime.";

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://497155]
Approved by lidden
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2021-07-23 14:42 GMT
Find Nodes?
    Voting Booth?

    No recent polls found