in reply to new way to code html?
?: should really be used to let Perl choose one or another value based on the truthfullness of an expression, not as a flow control mechanism. If you want flow control, use if and else
# Ok - pick a value based on the value of $bar $foo = $bar eq "baz" ? "foobaz" : "foobar"; print "I am the ", ($sex eq "male" ? "king" : "queen"), "of rock & rol +l"; some_subroutine( ref($foo) eq "ARRAY" ? $foo : [$foo] );
# Not ok - ?: flow control $bar eq "baz" ? $foo = "foo" : $bar = "baz";
# Ok - if/else flow control if ($bar eq "baz") { $foo = "foo"; } else { $bar = "baz"; }
# Ok - if/else flow control, if a bit verbose. if ($bar eq "baz") { $foo = "foo"; } else { $foo = "baz"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: new way to code html?
by tobyink (Canon) on Nov 21, 2012 at 07:05 UTC | |
by Anonymous Monk on Nov 21, 2012 at 08:23 UTC |