move perl code from class context into functions

This fixes issue 1958, so we can perl -c without need to worry about
code running