use strict; use warnings; use IO::File; { my $fh = IO::File->new( '< filename.txt' ); if( defined $fh ) { while( my $line = <$fh> ) { print $fh->input_line_number(), ":\t", $line; } } } # As $fh falls out of scope it is closed automatically.