in reply to Why do I (sometimes) get a REF ref and not a SCALAR ref?

The string is based on the referenced variable.

So,

  1. You are printing a reference to $a, which contains "foo": SCALAR
  2. You are printing a reference to $a, which contains \$a: REF
  3. You are printing a reference to $b, which contains "foo": SCALAR
  4. You are printing a reference to $a, which contains \$a: REF