You are returning 1 or 2 on error from the subroutine, but the return value from $f->close() on success, which is probably true (also 1).
I suggest you return 0 (zero) on success, and test the return value from the $th
n->join() for non-zero, so you can report an error. You should also change your error reporting (
print statements) to use STDERR or
warn instead of stdout.