...It looks like the error has something to do with this:
write(file, array)
I googled for examples of that syntax and found 2:
write(file, array, 4*count);
xfercount = write(file, array, rows * cols);
Maybe the errors are obvious to you once you take a closer look. I don't know PERL well...