Yes, using Encode::Guess might have worked too I suppose. That would require restricting myself to reading the whole input at once, but that wouldn't be too bad in this application anyway. However, if I read the whole input at once, that would have simplified my code a great deal too, and in that case I don't think Encode::Guess would have helped much compared to just testing for utf-8 and ascii input by hand. This code I can at least reuse later if I really need to read character encoded text data one line at a time in some program.