% perl -e '$i = 9; $p = \$i ; $q = \ $i; $i = "WTF"; print "$$p $$q\n"'
9 WTF
I was expecting that both work the same and print 9 11
, since neither the pre- nor the post-increment could be used as lvalues in perl.
Is this described anywhere, has it any rationale, or it's just a glitch? All I could find was about the magical properties of the increment operator when applied to non-numeric strings, which seems totally unrelated.
Notice that this also applies to decrement operators, it's not a matter of operator precedence (adding parens or writing it as ($p, $q) = \($i , $i)
does not change anything) and it's the same behaviour in older versions like perl 5.6.
I'm not asking about workarounds (I know about putting an int
between the \
and the
, or adding a zero, etc).
CodePudding user response:
Post-decrement returns the old value of $i
, so it must return a fresh scalar and does so.
Pre-decrement returns the new value of $i
, so it simply returns $i
. There's no point in building a new scalar for nothing.