Perl: Move encoder preparation to its own function

Refactoring to be able to add chunked support later.