Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^5: perltidy for ternary operator?

by atcroft (Abbot)
on Dec 07, 2014 at 04:51 UTC ( #1109464=note: print w/replies, xml ) Need Help??

in reply to Re^4: perltidy for ternary operator?
in thread perltidy for ternary operator?

Could one not just create a second ternary operation that perl would optimize out, such as the following?

print $coin ? q{heads} : 1 ? q{tails} : q{};

According to B:Deparse, the second ternary operator appears to be optimized out:

perl -MO=Deparse,-d \ -le \ 'my $coin = int(rand(2)); print $coin ? q{heads} : 1 ? q{tails} : q{};' BEGIN { $/ = "\n"; $\ = "\n"; } my $coin = int rand 2; print $coin ? "heads" : "tails"; -e syntax OK

However, the code, when handled by Perl::Tidy, appears to be formatted in the manner the OP is requesting:

perl -MPerl::Tidy -le ' my $code = q{my $coin = int(rand(2)); print $coin ? q{true} : 1 ? q{false} : q{};}; my $processed; Perl::Tidy::perltidy( source => \$code, destination => \$processed, argv => q{-npro -pbp -nst -se -bot -l=40}, ); print $processed;' my $coin = int( rand(2) ); print $coin ? q{true} : 1 ? q{false} : q{};

I know that may not be what the OP was hoping for, but perhaps it might be an option to look at.

Hope that helps.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1109464]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2021-02-26 21:34 GMT
Find Nodes?
    Voting Booth?

    No recent polls found