in reply to Re^4: Short form (ternary) if else
in thread Short form (ternary) if else
Both will work (i.e. run without error) but the first form won't do what you're expecting due to operator precedence. The second form is preferable anyway because it is shorter, less complicated and easier to read. Take a look at the perlop manpage - under Conditional Operator for an explanation.
I suggest you write some test code for yourself rather than just asking whether this or that code will or won't work. You can do this sort of thing on the commandline:
ken@ganymede: ~/tmp $ perl -Mstrict -Mwarnings -E 'my $x = 1 ? 1 : 0; say $x;' 1 ken@ganymede: ~/tmp $ perl -Mstrict -Mwarnings -E 'my $x; 1 ? $x=1 : $x=0; say $x;' 0 ken@ganymede: ~/tmp $ perl -Mstrict -Mwarnings -E 'my $x; 1 ? ($x=1) : ($x=0); say $x;' 1 ken@ganymede: ~/tmp $
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Short form (ternary) if else
by Riales (Hermit) on Feb 08, 2012 at 23:20 UTC | |
by wrog (Friar) on Feb 08, 2012 at 23:32 UTC | |
by Riales (Hermit) on Feb 08, 2012 at 23:43 UTC | |
by kcott (Archbishop) on Feb 08, 2012 at 23:59 UTC | |
by gg48gg (Sexton) on Feb 09, 2012 at 00:05 UTC |