File Coverage

blib/lib/App/nauniq.pm
Criterion Covered Total %
statement 9 56 16.0
branch 0 44 0.0
condition 0 3 0.0
subroutine 3 5 60.0
pod 0 1 0.0
total 12 109 11.0


line stmt bran cond sub pod time code
1             package App::nauniq;
2              
3 1     1   412767 use strict;
  1         3  
  1         206  
4 1     1   9 use warnings;
  1         2  
  1         435  
5              
6             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
7             our $DATE = '2025-09-24'; # DATE
8             our $DIST = 'App-nauniq'; # DIST
9             our $VERSION = '0.112'; # VERSION
10              
11             sub run {
12 0     0 0   my %opts = @_;
13              
14 0           my $ifh; # input handle
15 0 0         if (@ARGV) {
16 0           my $fname = shift @ARGV;
17 0 0         if ($fname eq '-') {
18 0           $ifh = *STDIN;
19             } else {
20 0 0         open $ifh, "<", $fname or die "Can't open input file $fname: $!\n";
21             }
22             } else {
23 0           $ifh = *STDIN;
24             }
25              
26 0           my $phase = 2;
27 0           my $ofh; # output handle
28 0 0         if (@ARGV) {
29 0           my $fname = shift @ARGV;
30 0 0         if ($fname eq '-') {
31 0           $ofh = *STDOUT;
32             } else {
33             open $ofh,
34 0 0         ($opts{read_output} ? "+" : "") . ($opts{append} ? ">>" : ">"),
    0          
    0          
35             $fname
36             or die "Can't open output file $fname: $!\n";
37 0 0         if ($opts{read_output}) {
38 0           seek $ofh, 0, 0;
39 0           $phase = 1;
40             }
41             }
42             } else {
43 0           $ofh = *STDOUT;
44             }
45              
46 0           my ($line, $memkey);
47 0           my %mem;
48             my $sub_reset_mem = sub {
49 0 0   0     if ($opts{num_entries} > 0) {
50 0           require Tie::Cache;
51 0           tie %mem, 'Tie::Cache', $opts{num_entries};
52             } else {
53 0           %mem = ();
54             }
55 0           };
56 0           $sub_reset_mem->();
57 0 0         require Digest::MD5 if $opts{md5};
58 1     1   9 no warnings; # we want to shut up 'substr outside of string'
  1         2  
  1         327  
59 0           while (1) {
60 0 0         if ($phase == 1) {
61             # phase 1 is just reading the output file
62 0           $line = <$ofh>;
63 0 0         if (!$line) {
64 0           $phase = 2;
65 0           next;
66             }
67             } else {
68 0           $line = <$ifh>;
69 0 0         if (!$line) {
70 0           last;
71             }
72             }
73 0 0 0       if ($opts{forget_pattern} && $line =~ $opts{forget_pattern}) {
74 0           $sub_reset_mem->();
75             }
76              
77             $memkey = $opts{check_chars} > 0 ?
78             substr($line, $opts{skip_chars}, $opts{check_chars}) :
79 0 0         substr($line, $opts{skip_chars});
80 0 0         $memkey = lc($memkey) if $opts{ignore_case};
81 0 0         $memkey = Digest::MD5::md5($memkey) if $opts{md5};
82              
83 0 0         if ($phase == 2) {
84 0 0         if ($mem{$memkey}) {
85 0 0         print $ofh $line if $opts{show_repeated};
86             } else {
87 0 0         print $ofh $line if $opts{show_unique};
88             }
89             }
90              
91 0           $mem{$memkey} = 1;
92             }
93             }
94              
95              
96             1;
97             # ABSTRACT: Non-adjacent uniq
98              
99             __END__