a grand renaming so that the most significant portion of the name comes first
This commit is contained in:
61
perl-practice/cli/ex03/rwfakegrep.pl
Normal file
61
perl-practice/cli/ex03/rwfakegrep.pl
Normal file
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use File::Basename;
|
||||
use English qw/$PROGRAM_NAME/;
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
23
perl-practice/cli/ex03/rwfiles.pl
Normal file
23
perl-practice/cli/ex03/rwfiles.pl
Normal file
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use File::Basename;
|
||||
|
||||
my $basename = basename($0);
|
||||
my $USAGE = <<USAGE
|
||||
Usage: $basename <infile> <outfile>
|
||||
USAGE
|
||||
;
|
||||
|
||||
open(INFILE, '<', $ARGV[0]) or die "$USAGE\n$!";
|
||||
open(OUTFILE, '>', $ARGV[1]) or die "$USAGE\n$!";
|
||||
|
||||
foreach my $inline (<INFILE>) {
|
||||
print OUTFILE $inline;
|
||||
}
|
||||
|
||||
|
||||
|
||||
1;
|
||||
__END__
|
Reference in New Issue
Block a user