my $fh = IO::File->new_tmpfile(); if (defined $fh) { # Do something with the tmpfile } else { # Do something in memory }