my $output = $twig->print;
I think you want
my $output = $twig->sprint;
there, so you get the string back instead of the twig object (thanks simbabque)
this will just output the variable as a JSON string though, not generate a data structure from the XML