@@ -22,20 +22,24 @@ use constant EX_FAILURE => 1;
22
22
our $VERSION = ' 1.3' ;
23
23
my $Program = basename($0 );
24
24
25
- getopts(' benstuv' , \my %options ) or do {
25
+ my %opt ;
26
+ getopts(' benstuv' , \%opt ) or do {
26
27
warn " usage: $Program [-benstuv] [file ...]\n " ;
27
28
exit EX_FAILURE;
28
29
};
29
30
30
- my $ends = exists $options {' e' };
31
- my $tabs = exists $options {' t' };
32
- my $nonprinting = exists $options {' v' } || $ends || $tabs ;
33
- my $squeeze_empty = exists $options {' s' };
34
- my $number_lines = $options {' n' } && !$options {' b' };
35
- my $number_non_blanks = exists $options {' b' };
31
+ my $ends = $opt {' e' };
32
+ my $tabs = $opt {' t' };
33
+ my $nonprinting = $opt {' v' } || $ends || $tabs ;
34
+ my $squeeze_empty = $opt {' s' };
35
+ my $number_lines = $opt {' n' } && !$opt {' b' };
36
+ my $number_non_blanks = $opt {' b' };
37
+
38
+ my $cook = grep {$_ || ()} @opt {' b' , ' e' , ' n' , ' s' , ' t' , ' v' };
39
+ my $cat = $cook ? \&cook_file : \&raw_file;
36
40
37
41
# Unbuffer output for -u.
38
- $| = exists $options {' u' };
42
+ $| = 1 if $opt {' u' };
39
43
40
44
my $was_empty = 0;
41
45
my $count = 0;
@@ -70,9 +74,28 @@ sub do_file {
70
74
return 1;
71
75
}
72
76
}
77
+ $cat -> ($fh );
73
78
74
- while (<$fh >) {
79
+ if ($name ne ' -' && !close ($fh )) {
80
+ warn " $Program : failed to close '$name ': $! \n " ;
81
+ return 1;
82
+ }
83
+ return 0;
84
+ }
85
+
86
+ sub raw_file {
87
+ my $fh = shift ;
88
+ my $BUFSZ = 8192;
89
+ my $buf ;
90
+ while (read $fh , $buf , $BUFSZ ) {
91
+ print $buf ;
92
+ }
93
+ }
94
+
95
+ sub cook_file {
96
+ my $fh = shift ;
75
97
98
+ while (<$fh >) {
76
99
if ($squeeze_empty ) {
77
100
my $is_empty = /^$/ ;
78
101
if ($is_empty && $was_empty ) {
@@ -96,11 +119,6 @@ sub do_file {
96
119
}
97
120
print ;
98
121
}
99
- if ($name ne ' -' && !close ($fh )) {
100
- warn " $Program : failed to close '$name ': $! \n " ;
101
- return 1;
102
- }
103
- return 0;
104
122
}
105
123
106
124
__END__
0 commit comments