my $expect = 'eyecatcher'; my $record; while (1) { if ($expect eq 'eyecatcher' || $expect eq 'eyecatcher_after_record') { if (read_two_bytes() eq '==') { process_record($record) if $expect eq 'eyecatcher_after_record'; $record = undef; $expect = 'length'; } else { die "expected eyecatcher" } } elsif ($expect eq 'length') { my $length = read_two_bytes(); $record = read_bytes($length); $expect = 'eyecater_after_record'; } }