in reply to seeking piped STDIO
The key part is the last if eof line. That tells process_invoice to return back to the main loop at the end of the current file. Without it, process_invoice would continue on to the next file automatically, because it is using <>.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; } }
The seek back in the main routine will ensure that the processor functions such as process_invoice see the entire file, including the first line.
|
|---|