Perl & C code follow: ----------------------------