Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2020-10-21 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (217 votes). Check out past polls.

    Notices?