in reply to Without trying; what do you think this would print?

Neither, looks like undefined behaviour if you ask me :) JOKING

Yup, looks like a bug if you ask me

$ perl -e " $a{0} ||= scalar keys %a; print $a{0} " 1 $ perl -e " $a{0} += scalar keys %a; print $a{0} " 1 $ perl -e " $a{0} = $a{0}+ scalar keys %a; print $a{0} " 0 $ perl -e " $a{0} = $a{0} || scalar keys %a; print $a{0} " 0
$ perl -MO=Concise -e " $a{0} ||= scalar keys %a; print $a{0} " j <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 - <1> null vK/1 ->c 7 <|> orassign(other->8) vK/1 ->c 6 <2> helem sKRM/2 ->7 4 <1> rv2hv sKR/1 ->5 3 <#> gv[*a] s ->4 5 <$> const[PV "0"] s ->6 b <1> sassign sK/BKWARD,1 ->c - <1> scalar sK/1 ->b a <1> keys[t4] sK/1 ->b 9 <1> rv2hv[t3] lKRM/1 ->a 8 <#> gv[*a] s ->9 c <;> nextstate(main 1 -e:1) v:{ ->d i <@> print vK ->j d <0> pushmark s ->e h <2> helem sK/2 ->i f <1> rv2hv sKR/1 ->g e <#> gv[*a] s ->f g <$> const[PV "0"] s ->h -e syntax OK
$ perl -MO=Concise -e " $a{0} = $a{0} || scalar keys %a; print $a{0} " n <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 f <2> sassign vKS/2 ->g - <1> null sK/1 ->b 7 <|> or(other->8) sK/1 ->b 6 <2> helem sK/2 ->7 4 <1> rv2hv sKR/1 ->5 3 <#> gv[*a] s ->4 5 <$> const[PV "0"] s ->6 - <1> scalar sK/1 ->- a <1> keys[t5] sK/1 ->b 9 <1> rv2hv[t4] lKRM/1 ->a 8 <#> gv[*a] s ->9 e <2> helem sKRM*/2 ->f c <1> rv2hv sKR/1 ->d b <#> gv[*a] s ->c d <$> const[PV "0"] s ->e g <;> nextstate(main 1 -e:1) v:{ ->h m <@> print vK ->n h <0> pushmark s ->i l <2> helem sK/2 ->m j <1> rv2hv sKR/1 ->k i <#> gv[*a] s ->j k <$> const[PV "0"] s ->l -e syntax OK

Replies are listed 'Best First'.
Re^2: Without trying; what do you think this would print?
by locked_user sundialsvc4 (Abbot) on Apr 30, 2011 at 12:32 UTC

    While it might be “inconsistent behavior,” it is also, clearly, “unpredictable code” that is dependent upon (and thus, vulnerable to) language implementation quirks.   If it matters what the language is to do, “when, and in what order,” then you are obliged to say so.   Otherwise, the optimizer can, and will, make sometimes-arbitrary decisions.   (“Heads, or tails?   If it doesn’t seem to matter, then I (the optimizer...) get to choose, and by the way, to do my job I must do so.”)   And when that happens, “whose fault is it, really?”   You knew ... you were warned.   Code like that is curious, maybe, but it’s also intrinsically defective.   It’s fun to play “Perl golf,” but don’t make your daily bread that way.

    (And although that statement as-writ might sound like some kind of weighty pronouncement ... I’m really in a very pleasant mood, with good music playing and good coffee in my hand.   It’s just an observation.)