Um, I tried both code samples, and tilly's worked fine, but merlyn's produces no output.
It seems to be always hitting the 'else' clause. Changing the readline ($_) to <$_> fixes it.
Update: jlp reports that merlyns code works for him. I'm running 5_005.03, whereas he's running 5.6.0. I don't know if that would make any difference or not.