open IN, $file or die "Cannot open $file for input: $!\n"; while( ) { # do your if-flag-two-step } close IN;