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

perltidy for ternary operator?

by TomDLux (Vicar)
on Dec 06, 2014 at 18:08 UTC ( #1109420=perlquestion: print w/replies, xml ) Need Help??

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

I'm using perltidy and would like to use Damian's PBP recommended layout for ternary operators:

my $var = ($condition ? $val_for_true : $val_for_false );

Yes, I like to parenthesize them. How can I get perltidy to generate that layout? I've tried a number of things, including '-pbp', but no such luck. What I get is:

my $var = ( $condition ? $val_for_true : val_for_false );

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Replies are listed 'Best First'.
Re: perltidy for ternary operator?
by LanX (Cardinal) on Dec 06, 2014 at 18:44 UTC
    I looked into PBP and that's not what it says!

    It recommends to

    Format cascaded ternary operators in columns
    and that's exactly what the -pbp switch does:

    lanx@lanx-1005HA:~$ cat /tmp/ my $var1 = $condition ? $val_for_true : $val_for_none ; my $var2 = $condition ? $val_for_true : $condition2 ? $val_for_ +true2 : $val_for_none ; lanx@lanx-1005HA:~$ perltidy -pbp /tmp/ my $var1 = $condition ? $val_for_true : $val_for_none; my $var2 = $condition ? $val_for_true : $condition2 ? $val_for_true2 : $val_for_none;

    I'd appreciate if you'd fix the typo in the title, provide links to documentation of the modules (like perltidy) and chapters (like "Ternaries") you are questioning.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      Page 32 of PBP, first edition : </p

      You can use the tabular layout even if you have only a singular ternary .... Starting out this way makes it easier for maintainers to subsequently add new alternatives to the table.

      As Occam said: Entia non sunt multiplicanda praeter necessitatem.

        That's a suggestion ("you can") for the case you expect new entries to come.

        But that's not a problem with perltidy, cause cascades (as demonstrated) would be fixed automatically.

        And I doubt that breaking simple ternaries would be appreciated by the majority.

        It might interest you that tidy's output of a simple ternaries depends on existing linebreaks: (see -bot option)

        lanx@lanx-1005HA:~$ cat /tmp/ my $var1 = $condition ? $val_for_true : $val_for_none ; #own linebreak my $var2 = $condition ? $val_for_true : $val_for_none ; lanx@lanx-1005HA:~$ perltidy -pbp /tmp/ my $var1 = $condition ? $val_for_true : $val_for_none; #own linebreak my $var2 = $condition ? $val_for_true : $val_for_none;

        Since the documentation mentions ternaries only once for -bot I doubt your goal can be achieved! (i.e. without patching the code)

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)


        ) i.a.W if you expect the ternary to be cascaded in the future then enter a linebreak manually.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1109420]
Approved by Perlbotics
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2021-04-15 06:38 GMT
Find Nodes?
    Voting Booth?

    No recent polls found