Following up on my earlier suggestion, if you
were to invoke the program as
script.pl files.*
instead of as
cat files.* | script.pl, then you
would still be able to use the convenient
<>
operator for reading the input. You might try something like this:
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 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
<>.
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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.