while (<>) { # examine the first line of the file # do the appropriate thing with it # (for example:) if (/^Expense Report/) { $type = 'Expense' } elsif (/^Invoice/) { $type = 'Invoice' } else { die ... } # Now seek the file seek(ARGV, 0, 0) or die "seek: $!"; process_data($type); # call the appropriate processor # the rest of the data in the file has been eaten } sub process_data { my ($type) = shift; # call process_invoice or process_expense_report # depending on $type } sub process_invoice { while (<>) { # do something last if eof; } }