Remove print $element->as_text from line 14. It prints the whole text, i.e. concatenation of all text nodes, which means it changes the order. Instead, print each text node when you encounter it in the recursion loop:
foreach my $child ($element->content_list) {
if (ref $child) {
wiki_render($child);
} else {
print $child;
}
}