script/urifind

filter: Statementstmt 77.7% 70 / 90 Branchbran 57.8% 22 / 38 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 77.7% 7 / 9 Podpod n/a totaltotal 68.7% 101 / 147 CC 1 SCAR 2.0
CC
1
Cov
70%
__ANON__
2.0
__ANON__
2.0
BEGIN
1.0
CRAP 1.2
« lib/URI/Find/Schemeless.pm ↑ Summary
1 #!/usr/local/bin/perl -w
2  
3 # ----------------------------------------------------------------------
4 # urifind - find URIs in a document and dump them to STDOUT.
5 # Copyright (C) 2003 darren chamberlain <darren@cpan.org>
6 # ----------------------------------------------------------------------
7  
8 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 our $VERSION = 20160806;
11  
12 9 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use URI::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use URI::Find::Schemeless;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # What to do, and how
19 9 my $help = 0;
20 9 my $version = 0;
21 9 my $sort = 0;
22 9 my $reverse = 0;
23 9 my $unique = 0;
24 9 my $prefix = 0;
25 9 my $noprefix = 0;
26 9 my @pats = ();
27 9 my @schemes = ();
28 9 my $dump = 0;
29 9 my $schemeless = 0;
30  
31 9 Getopt::Long::Configure(qw{no_ignore_case bundling});
32 GetOptions(
33 's!' => \$sort,
34 'u!' => \$unique,
35 'p!' => \$prefix,
36 'n!' => \$noprefix,
37 'r!' => \$reverse,
38 'h!' => \$help,
39 'v!' => \$version,
40 0 ▶ 'd!' => sub { $dump = 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 0 ▶ 'D!' => sub { $dump = 2 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 9 'P=s@' => \@pats,
43 'S=s@' => \@schemes,
44 'schemeless!' => \$schemeless,
45 );
46  
47 9 ▶if ($help || $version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help or $version) 0 9 9
Condition33%1/3
$help or $version
l!l&&r!l&&!r
009
MC/DC0%0/2
$help or $version
$help $version
Truth table33%1/3
A or B
A= $helpB= $version
ABresult
1X1
011
000
48 0 my $prog = basename($0);
49  
50 0 ▶ if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) { } 0 0 0
51 0 print <<HELP;
52 $prog - find URIs in a document and dump them to STDOUT.
53  
54 $prog [OPTIONS] file1 [file2[, file3[, ...]]]
55  
56 Options:
57  
58 -s Sort results.
59 -r Reverse sort results (implies -s).
60 -u Return unique results only.
61 -n Don't include filename in output.
62 -p Include filename in output (0 by default, but 1 if
63 multiple files are included on the command line).
64 -P \$re Print only lines matching regex '\$re'
65 (may be specified multiple times).
66 -S \$scheme Only this scheme (may be specified multiple times).
67 -h This help screen.
68 -v Display version and exit.
69 -d Dump compiled regexes and continue.
70 -D Dump compiled regexes and exit.
71  
72 HELP
73 }
74 else {
75 0 printf "$prog v.%.02f\n", $VERSION;
76 }
77  
78 0 exit(0);
79 }
80  
81 9 my (@uris, $count);
82 9 ▶unshift @ARGV, \*STDIN unless @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 1 8 9
83  
84 9 ▶if (($prefix + $noprefix) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix + $noprefix > 1) 0 9 9
85 0 my $prog = basename $0;
86 0 die "Can't specify -p and -n at the same time; try $prog -h\n";
87 }
88  
89  
90 # Print filename with matches? -p / -n
91 # If there is more than one file, then show filenames by
92 # default, unless explicitly asked not to (-n)
93 9 ▶if (@ARGV > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV > 1) { } 1 8 9
94 1 ▶ $prefix = 1 unless $noprefix;
Branch: 1 branch
BranchTrueFalseTotal
unless $noprefix 0 1 1
95 }
96 else {
97 8 ▶ $prefix = 0 unless $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix 7 1 8
98 }
99  
100 # Add schemes to the list of regexen
101 9 ▶if (@schemes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@schemes) 4 5 9
102 4 unshift @pats => sprintf '^(\b%s\b):' => join '\b|\b' => @schemes;
103 }
104  
105 # If we are dumping (-d, -D), then dump. Exit if -D.
106 9 ▶if ($dump) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dump) 0 9 9
107 0 ▶ print STDERR "\$scheme = '" . (defined $pats[0] ? $pats[0] : '') . "'\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $pats[0] ? : 0 0 0
108 0 print STDERR "\@pats = ('" . join("', '", @pats) . "')\n";
109 0 ▶ exit if $dump == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $dump == 2 0 0 0
110 }
111  
112 # Find the URIs
113 9 for my $argv (@ARGV) {
114 10 my ($name, $fh, $data);
115  
116 10 ▶ $argv = \*STDIN if ($argv eq '-');
Branch: 1 branch
BranchTrueFalseTotal
if $argv eq "-" 0 10 10
117  
118 10 ▶ if (ref $argv eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $argv eq 'GLOB') { } 1 9 10
119 1 local $/;
120 1 $data = <$argv>;
121 1 $name = '<stdin>'
122 }
123 else {
124 9 local $/;
125 9 ▶ $fh = IO::File->new($argv) or die "Can't open $argv: $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new($argv) or die "Can't open ${argv}: $!" 9 0 9
126 9 $data = <$fh>;
127 9 $name = $argv;
128 }
129  
130 10 ▶ my $class = $schemeless ? "URI::Find::Schemeless" : "URI::Find";
Branch: 1 branch
BranchTrueFalseTotal
$schemeless ? : 1 9 10
131 10 ▶ my $finder = $class->new(sub { push @uris => [ $name, $_[0] ] });
Subroutine __ANON__: called CC=1 SCAR=0.0
132 10 $finder->find(\$data);
133 }
134  
135 # Apply patterns, in @pats
136 9 for my $pat (@pats) {
137 5 @uris = grep { $_->[1] =~ /$pat/ } @uris;
138 }
139  
140 # Remove redundant links
141 9 ▶if ($unique) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unique) 0 9 9
142 0 my %unique;
143 0 @uris = grep { ++$unique{$_->[1]} == 1 } @uris;
144 }
145  
146 # Sort links, possibly in reverse
147 9 ▶if ($sort || $reverse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort or $reverse) 0 9 9
Condition33%1/3
$sort or $reverse
l!l&&r!l&&!r
009
MC/DC0%0/2
$sort or $reverse
$sort $reverse
Truth table33%1/3
A or B
A= $sortB= $reverse
ABresult
1X1
011
000
148 0 ▶ if ($reverse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reverse) { } 0 0 0
149 0 @uris = sort { $b->[1] cmp $a->[1] } @uris;
150 }
151 else {
152 0 @uris = sort { $a->[1] cmp $b->[1] } @uris;
153 }
154 }
155  
156 # Flatten the arrayrefs
157 9 ▶if ($prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix) { } 1 8 9
158 1 @uris = map { join ': ' => @$_ } @uris;
159 }
160 else {
161 8 @uris = map { $_->[1] } @uris;
162 }
163  
164 9 print map { "$_\n" } @uris;
165  
166 9 exit 0;
167  
168 __END__
« lib/URI/Find/Schemeless.pm ↑ Summary