my $prev; while( my $curr = <$infile> ) { chomp $curr; if( defined $prev && $curr eq $prev ) { # Take some action. } $prev = $curr; }