while (<>) { if (/name/) { print; my $next_line = <>; # do something with $next_line ... } } #### open my $fh, '<', $file or die "can not open $file: $!"; while <$fh> { ... }