http://qs321.pair.com?node_id=11119136


in reply to Ternary Quizical behaviour?

'b' => exists($tests{'b'}) && defined($m=$tests{'b'}) ? $m : 0,
needlessly superfulous and relies on a side effect I'm a little wary of; do:

'b' => (defined $tests{'b'}) ? $tests{'b'} : 0,
But since the default is 0, this should mitigate the need for defined:

b => $tests{b} // 0,

Also, defined($m=$tests{'b'}) smells a little funny; I get what you're doing but it is unnecessary.

Update - I am confused but the need for $m. It's getting set twice in your original code, both times based on the state of $tests{'b'}, then you set it to 10. What are you trying to do? Is this your real code or a contrived example?

Replies are listed 'Best First'.
Re^2: Ternary Quizical behaviour?
by Eily (Monsignor) on Jul 10, 2020 at 12:40 UTC

    But since the default is 0, this should mitigate the need for defined
    I don't understand what you mean here. As far as I know, defined $test{'a'} ? $test{'a'} : 0 and $test{'a'} // 0 are strictly equivalent.

    Beyond that ++ for the answer, the use of // here is a clear winner :)

      I just said what you made clear, but I did so in a very awkward way. Thanks for the ++! :)