In addition to the answer that dws
has already given, the original approach is better because it doesn't assume that the IRS was previously "\n", and it certainly doesn't put the IRS back as a literal \n
a newline character, because of the single quotes). The usual idiom for changing $/
is to wrap any changes to it in a block, and then localise within the block.
Also, maybe it's just my unfamiliarity with binmode, but I think that undef-ing the IRS means that the while loop only ever runs once.