Reimplement in Perl, add shared memory hooks

The shell script was getting pretty ugly :-(