Another option is to change your open statement so the filehandle of your temp file refers to an "in-memory" file. No other changes are required. Temporary data would go to a scalar instead of a disk.
Refer to 'open' in perlfunc for more on in-memory files.