in reply to Program can not read the contents of a small file that it has recently written!

The problem is the return statement in the while in the subroutine permute_bases. If you return here, the file is not closed and hence buffers are not flushed. If you then open the file and read from it, there is no garantee that what you wrote is already there. (Also note that the open in the subroutine has an unchecked return value - if the open fails, nothing would be written to the file either, but that's probably not the case).

If you would use an autovivified filehandle, using a lexical scalar, then the file would be closed if you perform the return - the filehandle goes out of scope, closing the file.

-- Abigail