my $last_state=''; while (my $line= ) { chomp $line; next unless ($line); if (substr($line,0,1) eq '+') { # if line starts with a plus use last_state to process process($last_state,substr($line,1)); } else { # otherwise set last_state and process with it my @parts=split(' ',$line,2); $last_state=$parts[0]; process($last_state,$parts[1]); } } # line sub process { my $state=shift; my $input=shift; ... do stuff based on $state and $input ... } # process