@@ -16,16 +16,20 @@ License: perl
16
16
17
17
use strict;
18
18
19
- my $VERSION = ' 1.0' ;
19
+ use constant EX_SUCCESS => 0;
20
+ use constant EX_FAILURE => 1;
21
+
22
+ my $VERSION = ' 1.1' ;
20
23
21
24
END {
22
25
close STDOUT || die " $0 : can't close stdout: $! \n " ;
23
26
$? = 1 if $? == 255; # from die
24
27
}
25
28
26
29
sub usage {
27
- print " usage: $0 [-c | -d | -u] [-f fields] [-s chars] [input files]\n " ;
28
- exit 1;
30
+ print " usage: $0 [-c | -d | -u] [-f fields] [-s chars] " ,
31
+ " [input_file [output_file]]\n " ;
32
+ exit EX_FAILURE;
29
33
}
30
34
31
35
sub version { print " $0 (Perl Power Tools) $VERSION \n " ; exit 0; }
@@ -58,12 +62,38 @@ while (@ARGV && $ARGV[0] =~ /^[-+]/) {
58
62
warn " $0 : invalid option -- $_ \n " ;
59
63
usage();
60
64
}
65
+ my $infile = shift ;
66
+ my $outfile = shift ;
67
+ if (@ARGV ) {
68
+ warn " $0 : unexpected argument: '$ARGV [0]'\n " ;
69
+ usage();
70
+ }
71
+ my ($fh , $out , $comp , $save_comp , $line , $save_line , $count , $eof );
61
72
62
- my ($comp , $save_comp , $line , $save_line , $count , $eof );
73
+ if (defined $infile ) {
74
+ if (-d $infile ) {
75
+ warn " $0 : '$infile ' is a directory\n " ;
76
+ exit EX_FAILURE;
77
+ }
78
+ unless (open $fh , ' <' , $infile ) {
79
+ warn " $0 : failed to open '$infile ': $! \n " ;
80
+ exit EX_FAILURE;
81
+ }
82
+ } else {
83
+ $fh = *STDIN ;
84
+ }
85
+ if (defined $outfile ) {
86
+ unless (open $out , ' >' , $outfile ) {
87
+ warn " $0 : failed to open '$outfile ': $! \n " ;
88
+ exit EX_FAILURE;
89
+ }
90
+ } else {
91
+ $out = *STDOUT ;
92
+ }
63
93
64
94
# prime the pump
65
- $comp = $line = <>;
66
- exit 0 unless defined $line ;
95
+ $comp = $line = <$fh >;
96
+ exit EX_SUCCESS unless defined $line ;
67
97
if ($optf ) {($comp ) = (split ' ' , $comp , $optf +1)[$optf ] }
68
98
if ($opts ) { $comp = substr ($comp , $opts ) }
69
99
@@ -73,22 +103,22 @@ while (!$eof) {
73
103
$save_comp = $comp ;
74
104
$count = 1;
75
105
DUPS:
76
- while (!($eof = eof ())) {
77
- $comp = $line = <>;
106
+ while (!($eof = eof ($fh ))) {
107
+ $comp = $line = <$fh >;
78
108
if ($optf ) {($comp ) = (split ' ' , $comp , $optf +1)[$optf ] }
79
109
if ($opts ) { $comp = substr ($comp , $opts ) }
80
110
last DUPS if $comp ne $save_comp ;
81
111
++$count ;
82
112
}
83
113
# when we get here, $save_line is the first occurrence of a sequence
84
114
# of duplicate lines, $count is the number of times it appears
85
- if ($optc ) { printf " %7d $save_line " , $count }
86
- elsif ($optd ) { print $save_line if $count > 1 }
87
- elsif ($optu ) { print $save_line if $count == 1 }
88
- else { print $save_line }
115
+ if ($optc ) { printf { $out } ' %7d %s ' , $count , $save_line }
116
+ elsif ($optd ) { print { $out } $save_line if $count > 1 }
117
+ elsif ($optu ) { print { $out } $save_line if $count == 1 }
118
+ else { print { $out } $save_line }
89
119
}
90
120
91
- exit 0 ;
121
+ exit EX_SUCCESS ;
92
122
93
123
__END__
94
124
@@ -98,7 +128,8 @@ uniq - report or filter out repeated lines in a file
98
128
99
129
=head1 SYNOPSIS
100
130
101
- uniq [B<-c > | B<-d > | B<-u > ] [B<-f > I<fields > ] [B<-s > I<chars > ] [I<input files > ]
131
+ uniq [B<-c > | B<-d > | B<-u > ] [B<-f > I<fields > ] [B<-s > I<chars > ]
132
+ [input_file [output_file]]
102
133
103
134
=head1 DESCRIPTION
104
135
0 commit comments