in reply to match a word then exit

while (<>) { exit if /error.*error/; # do whatever you want to do... }

(if you just want to exit the loop - not the program - use last in place of exit)