Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Perl::Tidy formatting question

by pukku (Novice)
on Oct 26, 2011 at 16:33 UTC ( #933933=perlquestion: print w/ replies, xml ) Need Help??
pukku has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

I have a question about a particular issue with Perl::Tidy.

If I have the following code:

my $res = SOAP::Lite->autotype(0)->uri('http:...')->proxy('http:...')- +>function_call();

I would like the result to be:

my $res = SOAP::Lite ->autotype(0) ->uri('http:....') ->proxy('http:...') ->function_call();

Instead, I get:

my $res = SOAP::Lite->autotype(0)->uri('http:...') ->proxy('http:...') ->function_call();

The breaks have to do with line length, obviously, but my question is how do I tell Perl::Tidy that it should put the object on the same line as the assignment -- that is, if it's going to break lines with chained ->, it should do so for all of them?

Thanks.

Comment on Perl::Tidy formatting question
Select or Download Code
Re: Perl::Tidy formatting question
by Anonymous Monk on Oct 26, 2011 at 17:26 UTC
    unfortunately perltidy has many bugs and isn't updated very frequently, so don't place all your faith in it. see the large buglist here: https://rt.cpan.org/Public/Dist/Display.html?Name=Perl-Tidy

      see the large buglist here

      <sarcasm> wow 30 bugs, what an ginormous number for something as simple as perl syntax, egads

Re: Perl::Tidy formatting question
by jmcnamara (Monsignor) on Oct 26, 2011 at 17:41 UTC

    The PerlTidy manpage doesn't mention chained methods so I guess it doesn't handle them.

    As a workaround you could format the code manually and then tell it to ignore that block with PerlTidy --format-skipping (#<<< #>>>) comments:

    #<<< my $res = SOAP::Lite ->autotype(0) ->uri('http:....') ->proxy('http:...') ->function_call(); #>>>

    Of course if you have hundreds of instances like this then that will quickly become a pain.

    You can also force alignment by using comments at the end of each line or newlines between them (but neither of those has much advantage over the above):

    my $res = SOAP::Lite # ->autotype( 0 ) # ->uri( 'http:....' ) # ->proxy( 'http:...' ) # ->function_call(); #

    --
    John.

      Thanks. That's what I thought, given my reading through the man page, but I was hoping someone knew something I didn't...

Re: Perl::Tidy formatting question
by vendion (Scribe) on Nov 22, 2011 at 16:13 UTC

    I also have a question on Perl::Tidy's formatting, one complaint I have is the new line for else statements. So a if/else statement that looks like this:

    if ( $foo eq 'bar' ) { # Do something } else { # Do something else }
    Which in my option is perfectly readable becomes this once ran through Perl::Tidy:
    if ( $foo eq 'bar' ) { # Do something } else { # Do something else }
    I don't quite get the reason for breaking the else statement in such a way and if there is a way to tell it not to do such, I may have over looked it when reading through the man for it, please let me know. Also if there is some really good reason why Perl::Tidy would default to doing this that is considered best practice and doesn't cause Perl::Critic on its most brutal setting to complain about it please enlighten me.

      if there is a way to tell it not to do such, I may have over looked it when reading through the man for it, please let me know
      You want "cuddled else" (-ce):
      perltidy -ce script.pl
      The Perl Best Practices book recommends: "Don't cuddle an else". You need to buy the book if you want to know the reason. Perhaps perltidy adheres to this as its default behavior.

        I do have the PBP I guess I have not run across that before, what is even weirder is Perl::Critic doesn't say anything under Brutal mode and that basically "throws the book at you". Anyways thanks for the pointer and I'll have to search for the section of the PBP later today for why that is recommended.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://933933]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-11-28 20:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (200 votes), past polls