Fix bad file descriptor error on some Perl versions