Perl support

Both C and Perl versions have better structure.