OK - the problem was what you mentioned, that split is returning two scalars. The first 0 character result is passing through the conditional check and messing things up - it's not the xml declaration, as I thought.
By the way, I tried to abbreviate how the string $out was populated and forgot it had quotes in it, so code as posted would have failed (as you and others noticed).
The loop works now - thanks much you guys rock!
As for the use of substr instead of regex.. well I was mostly doing it for consistency, because substr works so well for isolating pieces of the xml elements. But, efficiency is more important so I'll change it - thanks for the suggestion :)