box-o-sand/cli/ex03/rwfakegrep.pl

62 lines
1.2 KiB
Perl
Raw Normal View History

2011-01-07 04:22:14 +00:00
#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;
use English qw/$PROGRAM_NAME/;
2011-01-07 04:22:14 +00:00
my $basename = basename($0);
my $USAGE = <<USAGE
Usage: $basename <infile> <outfile>
USAGE
;
sub show_last_chapter_with_nlines {
my ($ch, $nlines) = @_;
print "Chapter $ch has $nlines lines\n";
2011-01-07 04:22:14 +00:00
}
sub main {
open(INFILE, '<', $ARGV[0]) or die "$USAGE\n$!";
open(OUTFILE, '>', $ARGV[1]) or die "$USAGE\n$!";
my $have_seen_firstchapter = 'no';
my $nlines = 0;
my $chline = '';
my $ch = '';
foreach my $inline (<INFILE>) {
if ($inline =~ /^\s*CHAPTER.*$/) {
$chline = "$inline";
$ch = "$chline";
$ch =~ s/^\s*CHAPTER\s*([0-9]*).*/$1/;
$ch =~ s/[\r\n]//;
if ($have_seen_firstchapter eq 'yes') {
show_last_chapter_with_nlines($ch - 1, $nlines);
$nlines = 0;
} else {
$have_seen_firstchapter = 'yes';
$nlines = 0;
}
print OUTFILE $inline;
}
$nlines++;
}
show_last_chapter_with_nlines($ch, $nlines);
}
if ($PROGRAM_NAME eq __FILE__) {
main();
}
2011-01-07 04:22:14 +00:00
1;
__END__