in reply to Detecting last line and blank line
The eof operator is useful here.
#!/usr/bin/env perl use strict; use warnings; my $fh = \*DATA; while(<$fh>) { chomp; print "<<<$_>>> is line $."; print ", and is the last line." if eof($fh); } continue { print ".\n"; } __DATA__ line 1 line 2 line 3
This produces the following output:
<<<line 1>>> is line 1. <<<line 2>>> is line 2. <<<line 3>>> is line 3, and is the last line.
My purpose in copying \*DATA into $fh was just so that the remainder of my example could operate on a filehandle that is closer to what one might see in typical code.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Detecting last line and blank line
by haukex (Archbishop) on Nov 07, 2017 at 05:27 UTC |