while(<>) { my($val); if(/^(\w+?)/) { ($val) = $1; } else { # Handle failed regex. } # Do something with $val. }