Then redo is just for that! Unfortunately you can't use it as simply as in
redo if eof;
since eof will continue to hold true and you won't run the loop just one last time. You'll have to add some more custom control instead:
my ($i,$cnt);
while (<$inhandle>) {
print ++$i;
last if $cnt;
$cnt++, redo if eof;
}
To add to the other suggestions you have been given and with a big caveat: we always recommend to always read files in line by line and not to slurp them in all at once. But if you're confident your input will not be exceedingly large in size and/or you collect all lines anyway, you may just do the latter:
for (<$infile>,'AFTERFILE') { ... }
|