lib/URI/Find.pm

filter: Statementstmt 96.7% 147 / 152 Branchbran 72.4% 42 / 58 Conditioncond 74.0% 20 / 27 MC/DCmcdc 50.0% 9 / 18 Subroutinesub 95.6% 22 / 23 Podpod 84.6% 11 / 13 totaltotal 86.2% 251 / 291 CC 39 SCAR 16.4
CC
39
Cov
88%
badinvo
12.0
_is_uri
9.1
find
9.0
CRAP 5.2
↑ Summary lib/URI/Find/Schemeless.pm »
1 package URI::Find;
2  
3 15 require 5.006;
4  
5 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use vars qw($VERSION @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 15 $VERSION = 20160806;
11 15 @EXPORT = qw(find_uris);
12  
13 15 ▶use constant YES => (1==1);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use constant NO => !YES;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 15 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 15 require URI;
19  
20 15 my $reserved = q(;/?:@&=+$,[]);
21 15 my $mark = q(-_.!~*'());
22 15 my $unreserved = "A-Za-z0-9\Q$mark\E";
23 15 my $uric = quotemeta($reserved) . '\p{isAlpha}' . $unreserved . "%";
24  
25 # URI scheme pattern without the non-alpha numerics.
26 # Those are extremely uncommon and interfere with the match.
27 15 my($schemeRe) = qr/[a-zA-Z][a-zA-Z0-9\+]*/;
28 15 my($uricSet) = $uric; # use new set
29  
30 # Some schemes which URI.pm does not explicitly support.
31 15 my $extraSchemesRe = qr{^(?:git|svn|ssh|svn\+ssh)$};
32  
33 # We need to avoid picking up 'HTTP::Request::Common' so we have a
34 # subset of uric without a colon ("I have no colon and yet I must poop")
35 15 my($uricCheat) = __PACKAGE__->uric_set;
36 15 $uricCheat =~ tr/://d;
37  
38 # Identifying characters accidentally picked up with a URI.
39 15 my($cruftSet) = q{])\},.'";}; #'#
40  
41  
42 =head1 NAME
43  
44 URI::Find - Find URIs in arbitrary text
45  
46 =head1 SYNOPSIS
47  
48 require URI::Find;
49  
50 my $finder = URI::Find->new(\&callback);
51  
52 $how_many_found = $finder->find(\$text);
53  
54 =head1 DESCRIPTION
55  
56 This module does one thing: Finds URIs and URLs in plain text. It
57 finds them quickly and it finds them B<all> (or what URI.pm considers
58 a URI to be.) It only finds URIs which include a scheme (http:// or
59 the like), for something a bit less strict have a look at
60 L<URI::Find::Schemeless|URI::Find::Schemeless>.
61  
62 For a command-line interface, L<urifind> is provided.
63  
64 =head2 Public Methods
65  
66 =over 4
67  
68 =item B<new>
69  
70 my $finder = URI::Find->new(\&callback);
71  
72 Creates a new URI::Find object.
73  
74 &callback is a function which is called on each URI found. It is
75 passed two arguments, the first is a URI object representing the URI
76 found. The second is the original text of the URI found. The return
77 value of the callback will replace the original URI in the text.
78  
79 =cut
80  
81 218 ▶sub new {
Subroutine new: called CC=3 SCAR=11.6
Pod documented
82 218 ▶ @_ == 2 || __PACKAGE__->badinvo;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or __PACKAGE__->badinvo 218 0 218
83 218 my($proto, $callback) = @_;
84 218 ▶ my($class) = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
02180
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
85 218 my $self = bless {}, $class;
86  
87 218 $self->{callback} = $callback;
88  
89 218 return $self;
90 }
91  
92 =item B<find>
93  
94 my $how_many_found = $finder->find(\$text);
95  
96 $text is a string to search and possibly modify with your callback.
97  
98 Alternatively, C<find> can be called with a replacement function for
99 the rest of the text:
100  
101 use CGI qw(escapeHTML);
102 # ...
103 my $how_many_found = $finder->find(\$text, \&escapeHTML);
104  
105 will not only call the callback function for every URL found (and
106 perform the replacement instructions therein), but also run the rest
107 of the text through C<escapeHTML()>. This makes it easier to turn
108 plain text which contains URLs into HTML (see example below).
109  
110 =cut
111  
112 221 ▶sub find {
Subroutine find: called CC=9 SCAR=22.0
Pod documented
113 221 ▶ @_ == 2 || @_ == 3 || __PACKAGE__->badinvo;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or @_ == 3 or __PACKAGE__->badinvo 221 0 221
Condition66%2/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
204170
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table66%2/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
114 221 my($self, $r_text, $escape_func) = @_;
115  
116 # Might be slower, but it makes the code simpler
117 221 ▶ $escape_func ||= sub { return $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$escape_func ||= sub { return $_[0]; }
l!l&&r!l&&!r
172040
MC/DC0%0/2
$escape_func ||= sub { return $_[0]; }
$escape_func sub { return $_[0]; }
Truth table66%2/3
A ||= B
A= $escape_funcB= sub { return $_[0]; }
ABresult
1X1
011
000
118  
119 # Store the escape func in the object temporarily for use
120 # by other methods.
121 221 local $self->{escape_func} = $escape_func;
122  
123 221 $self->{_uris_found} = 0;
124  
125 # Yes, evil. Basically, look for something vaguely resembling a URL,
126 # then hand it off to URI for examination. If it passes, throw
127 # it to a callback and put the result in its place.
128 221 local $SIG{__DIE__} = 'DEFAULT';
129 221 my $uri_cand;
130 my $uri;
131  
132 221 my $uriRe = sprintf '(?:%s|%s)', $self->uri_re, $self->schemeless_uri_re;
133  
134 221 $$r_text =~ s{ (.*?) (?:(<(?:URL:)?)(.+?)(>)|($uriRe)) | (.+?)$ }{
135 580 my $replace = '';
136 580 ▶ if( defined $6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $6) { } 127 453 580
137 127 $replace = $escape_func->($6);
138 }
139 else {
140 453 my $maybe_uri = '';
141  
142 453 ▶ $replace = $escape_func->($1) if length $1;
Branch: 1 branch
BranchTrueFalseTotal
if length $1 369 84 453
143  
144 453 ▶ if( defined $2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2) { } 114 339 453
145 114 $maybe_uri = $3;
146 114 my $is_uri = do { # Don't alter $1...
147 114 $maybe_uri =~ s/\s+//g;
148 114 $maybe_uri =~ /^$uriRe/;
149 };
150  
151 114 ▶ if( $is_uri ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_uri) { } 63 51 114
152 63 $replace .= $escape_func->($2);
153 63 $replace .= $self->_uri_filter($maybe_uri);
154 63 $replace .= $escape_func->($4);
155 }
156 else {
157 # the whole text inside of the <...> was not a url, but
158 # maybe it has a url (like an HTML <a> link)
159 51 my $has_uri = do { # Don't alter $1...
160 51 $maybe_uri = $3;
161 51 $maybe_uri =~ /$uriRe/;
162 };
163 51 ▶ if( $has_uri ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_uri) { } 4 47 51
164 4 my $pre = $2;
165 4 my $post = $4;
166 4 do { $self->find(\$maybe_uri, $escape_func) };
167 4 $replace .= $escape_func->($pre);
168 4 $replace .= $maybe_uri; # already escaped by find()
169 4 $replace .= $escape_func->($post);
170 }
171 else {
172 47 $replace .= $escape_func->($2.$3.$4);
173 }
174 }
175 }
176 else {
177 339 $replace .= $self->_uri_filter($5);
178 }
179 }
180  
181 580 $replace;
182 }gsex;
183  
184 221 return $self->{_uris_found};
185 }
186  
187  
188 402 ▶sub _uri_filter {
Subroutine _uri_filter: called CC=2 SCAR=6.9
189 402 my($self, $orig_match) = @_;
190  
191 # A heuristic. Often you'll see things like:
192 # "I saw this site, http://www.foo.com, and its really neat!"
193 # or "Foo Industries (at http://www.foo.com)"
194 # We want to avoid picking up the trailing paren, period or comma.
195 # Of course, this might wreck a perfectly valid URI, more often than
196 # not it corrects a parse mistake.
197 402 $orig_match = $self->decruft($orig_match);
198  
199 402 my $replacement = '';
200 402 ▶ if( my $uri = $self->_is_uri(\$orig_match) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $uri = $self->_is_uri(\$orig_match)) { } 387 15 402
201 # It's a URI
202 387 $self->{_uris_found}++;
203 387 $replacement = $self->{callback}->($uri, $orig_match);
204 }
205 else {
206 # False alarm
207 15 $replacement = $self->{escape_func}->($orig_match);
208 }
209  
210 # Return recrufted replacement
211 402 return $self->recruft($replacement);
212 }
213  
214  
215 =back
216  
217 =head2 Protected Methods
218  
219 I got a bunch of mail from people asking if I'd add certain features
220 to URI::Find. Most wanted the search to be less restrictive, do more
221 heuristics, etc... Since many of the requests were contradictory, I'm
222 letting people create their own custom subclasses to do what they
223 want.
224  
225 The following are methods internal to URI::Find which a subclass can
226 override to change the way URI::Find acts. They are only to be called
227 B<inside> a URI::Find subclass. Users of this module are NOT to use
228 these methods.
229  
230 =over
231  
232 =item B<uri_re>
233  
234 my $uri_re = $self->uri_re;
235  
236 Returns the regex for finding absolute, schemed URIs
237 (http://www.foo.com and such). This, combined with
238 schemeless_uri_re() is what finds candidate URIs.
239  
240 Usually this method does not have to be overridden.
241  
242 =cut
243  
244 221 ▶sub uri_re {
Subroutine uri_re: called CC=2 SCAR=7.1
Pod documented
245 221 ▶ @_ == 1 || __PACKAGE__->badinvo;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or __PACKAGE__->badinvo 221 0 221
246 221 my($self) = shift;
247 221 return sprintf '%s:[%s][%s#]*', $schemeRe,
248 $uricCheat,
249 $self->uric_set;
250 }
251  
252 =item B<schemeless_uri_re>
253  
254 my $schemeless_re = $self->schemeless_uri_re;
255  
256 Returns the regex for finding schemeless URIs (www.foo.com and such) and
257 other things which might be URIs. By default this will match nothing
258 (though it used to try to find schemeless URIs which started with C<www>
259 and C<ftp>).
260  
261 Many people will want to override this method. See L<URI::Find::Schemeless>
262 for a subclass does a reasonable job of finding URIs which might be missing
263 the scheme.
264  
265 =cut
266  
267 426 ▶sub schemeless_uri_re {
Subroutine schemeless_uri_re: called CC=2 SCAR=7.1
Pod documented
268 426 ▶ @_ == 1 || __PACKAGE__->badinvo;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or __PACKAGE__->badinvo 426 0 426
269 426 my($self) = shift;
270 426 return qr/\b\B/; # match nothing
271 }
272  
273 =item B<uric_set>
274  
275 my $uric_set = $self->uric_set;
276  
277 Returns a set matching the 'uric' set defined in RFC 2396 suitable for
278 putting into a character set ([]) in a regex.
279  
280 You almost never have to override this.
281  
282 =cut
283  
284 247 ▶sub uric_set {
Subroutine uric_set: called CC=2 SCAR=7.2
Pod documented
285 247 ▶ @_ == 1 || __PACKAGE__->badinvo;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or __PACKAGE__->badinvo 247 0 247
286 247 return $uricSet;
287 }
288  
289 =item B<cruft_set>
290  
291 my $cruft_set = $self->cruft_set;
292  
293 Returns a set of characters which are considered garbage. Used by
294 decruft().
295  
296 =cut
297  
298 11 ▶sub cruft_set {
Subroutine cruft_set: called CC=2 SCAR=7.2
Pod documented
299 11 ▶ @_ == 1 || __PACKAGE__->badinvo;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or __PACKAGE__->badinvo 11 0 11
300 11 return $cruftSet;
301 }
302  
303 =item B<decruft>
304  
305 my $uri = $self->decruft($uri);
306  
307 Sometimes garbage characters like periods and parenthesis get
308 accidentally matched along with the URI. In order for the URI to be
309 properly identified, it must sometimes be "decrufted", the garbage
310 characters stripped.
311  
312 This method takes a candidate URI and strips off any cruft it finds.
313  
314 =cut
315  
316 15 my %balanced_cruft = (
317 '(' => ')',
318 '{' => '}',
319 '[' => ']',
320 '"' => '"',
321 q['] => q['],
322 );
323  
324 402 ▶sub decruft {
Subroutine decruft: called CC=7 SCAR=19.5
Pod documented
325 402 ▶ @_ == 2 || __PACKAGE__->badinvo;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or __PACKAGE__->badinvo 402 0 402
326 402 my($self, $orig_match) = @_;
327  
328 402 $self->{start_cruft} = '';
329 402 $self->{end_cruft} = '';
330  
331 402 ▶ if( $orig_match =~ s/([\Q$cruftSet\E]+)$// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_match =~ s/([\Q$cruftSet\E]+)$//) 63 339 402
332 # urls can end with HTML entities if found in HTML so let's put back semicolons
333 # if this looks like the case
334 63 my $cruft = $1;
335 63 ▶ if( $cruft =~ /^;/ && $orig_match =~ /\&(\#[1-9]\d{1,3}|[a-zA-Z]{2,8})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cruft =~ /^;/ and $orig_match =~ /\&(\#[1-9]\d{1,3}|[a-zA-Z]{2,8})$/) 1 62 63
Condition66%2/3
$cruft =~ /^;/ and $orig_match =~ /\&(\#[1-9]\d{1,3}|[a-zA-Z]{2,8})$/
!ll&&!rl&&r
6201
MC/DC50%1/2
$cruft =~ /^;/ and $orig_match =~ /\&(\#[1-9]\d{1,3}|[a-zA-Z]{2,8})$/
$cruft =~ /^;/ $orig_match =~ /\&(\#[1-9]\d{1,3}|[a-zA-Z]{2,8})$/
Truth table66%2/3
A and B
A= $cruft =~ /^;/B= $orig_match =~ /\&(\#[1-9]\d{1,3}|[a-zA-Z]{2,8})$/
ABresult
0X0
100
111
336 1 $orig_match .= ';';
337 1 $cruft =~ s/^;//;
338 }
339  
340 63 ▶ while( my($open, $close) = each %balanced_cruft ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %balanced_cruft) 315 63 378
341 315 $self->recruft_balanced(\$orig_match, \$cruft, $open, $close);
342 }
343  
344 63 ▶ $self->{end_cruft} = $cruft if $cruft;
Branch: 1 branch
BranchTrueFalseTotal
if $cruft 50 13 63
345 }
346  
347 402 return $orig_match;
348 }
349  
350  
351 315 ▶sub recruft_balanced {
Subroutine recruft_balanced: called CC=3 SCAR=11.0
Pod undocumented
352 315 my $self = shift;
353 315 my($orig_match, $cruft, $open, $close) = @_;
354  
355 315 my $open_count = () = $$orig_match =~ m{\Q$open}g;
356 315 my $close_count = () = $$orig_match =~ m{\Q$close}g;
357  
358 315 ▶ if ( $$cruft =~ /\Q$close\E$/ && $open_count == ( $close_count + 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$cruft =~ /\Q$close\E$/ and $open_count == $close_count + 1) 18 297 315
Condition100%3/3
$$cruft =~ /\Q$close\E$/ and $open_count == $close_count + 1
!ll&&!rl&&r
2732418
MC/DC100%2/2
$$cruft =~ /\Q$close\E$/ and $open_count == $close_count + 1
$$cruft =~ /\Q$close\E$/ $open_count == $close_count + 1
Truth table100%3/3
A and B
A= $$cruft =~ /\Q$close\E$/B= $open_count == $close_count + 1
ABresult
0X0
100
111
359 18 $$orig_match .= $close;
360 18 $$cruft =~ s/\Q$close\E$//;
361 }
362  
363 315 return;
364 }
365  
366  
367 =item B<recruft>
368  
369 my $uri = $self->recruft($uri);
370  
371 This method puts back the cruft taken off with decruft(). This is necessary
372 because the cruft is destructively removed from the string before invoking
373 the user's callback, so it has to be put back afterwards.
374  
375 =cut
376  
377 #'#
378  
379 402 ▶sub recruft {
Subroutine recruft: called CC=2 SCAR=7.1
Pod documented
380 402 ▶ @_ == 2 || __PACKAGE__->badinvo;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or __PACKAGE__->badinvo 402 0 402
381 402 my($self, $uri) = @_;
382  
383 402 return $self->{start_cruft} . $uri . $self->{end_cruft};
384 }
385  
386 =item B<schemeless_to_schemed>
387  
388 my $schemed_uri = $self->schemeless_to_schemed($schemeless_uri);
389  
390 This takes a schemeless URI and returns an absolute, schemed URI. The
391 standard implementation supplies ftp:// for URIs which start with ftp.,
392 and http:// otherwise.
393  
394 =cut
395  
396 47 ▶sub schemeless_to_schemed {
Subroutine schemeless_to_schemed: called CC=3 SCAR=11.0
Pod documented
397 47 ▶ @_ == 2 || __PACKAGE__->badinvo;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or __PACKAGE__->badinvo 47 0 47
398 47 my($self, $uri_cand) = @_;
399  
400 47 ▶ $uri_cand =~ s|^(<?)ftp\.|$1ftp://ftp\.|
Branch: 1 branch
BranchTrueFalseTotal
$uri_cand =~ s[^(<?)ftp\.][$1ftp://ftp.] or $uri_cand =~ s[^(<?)][$1http://] 6 41 47
401 or $uri_cand =~ s|^(<?)|${1}http://|;
402  
403 47 return $uri_cand;
404 }
405  
406 =item B<is_schemed>
407  
408 $obj->is_schemed($uri);
409  
410 Returns whether or not the given URI is schemed or schemeless. True for
411 schemed, false for schemeless.
412  
413 =cut
414  
415 2 ▶sub is_schemed {
Subroutine is_schemed: called CC=2 SCAR=7.1
Pod documented
416 2 ▶ @_ == 2 || __PACKAGE__->badinvo;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or __PACKAGE__->badinvo 2 0 2
417 2 my($self, $uri) = @_;
418 2 return scalar $uri =~ /^<?$schemeRe:/;
419 }
420  
421 =item I<badinvo>
422  
423 __PACKAGE__->badinvo($extra_levels, $msg)
424  
425 This is used to complain about bogus subroutine/method invocations.
426 The args are optional.
427  
428 =cut
429  
430 0 ▶sub badinvo {
Subroutine badinvo: not called CC=3 SCAR=24.8
Pod documented
431 0 my $package = shift;
432 0 ▶ my $level = @_ ? shift : 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
433 0 ▶ my $msg = @_ ? " (" . shift() . ")" : '';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
434 0 my $subname = (caller $level + 1)[3];
435 0 croak "Bogus invocation of $subname$msg";
436 }
437  
438 =back
439  
440 =head2 Old Functions
441  
442 The old find_uri() function is still around and it works, but its
443 deprecated.
444  
445 =cut
446  
447 # Old interface.
448 64 ▶sub find_uris (\$&) {
Subroutine find_uris: called CC=2 SCAR=7.0
Pod undocumented
449 64 ▶ @_ == 2 || __PACKAGE__->badinvo;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or __PACKAGE__->badinvo 64 0 64
450 64 my($r_text, $callback) = @_;
451  
452 64 my $self = __PACKAGE__->new($callback);
453 64 return $self->find($r_text);
454 }
455  
456  
457 =head1 EXAMPLES
458  
459 Store a list of all URIs (normalized) in the document.
460  
461 my @uris;
462 my $finder = URI::Find->new(sub {
463 my($uri) = shift;
464 push @uris, $uri;
465 });
466 $finder->find(\$text);
467  
468 Print the original URI text found and the normalized representation.
469  
470 my $finder = URI::Find->new(sub {
471 my($uri, $orig_uri) = @_;
472 print "The text '$orig_uri' represents '$uri'\n";
473 return $orig_uri;
474 });
475 $finder->find(\$text);
476  
477 Check each URI in document to see if it exists.
478  
479 use LWP::Simple;
480  
481 my $finder = URI::Find->new(sub {
482 my($uri, $orig_uri) = @_;
483 if( head $uri ) {
484 print "$orig_uri is okay\n";
485 }
486 else {
487 print "$orig_uri cannot be found\n";
488 }
489 return $orig_uri;
490 });
491 $finder->find(\$text);
492  
493  
494 Turn plain text into HTML, with each URI found wrapped in an HTML anchor.
495  
496 use CGI qw(escapeHTML);
497 use URI::Find;
498  
499 my $finder = URI::Find->new(sub {
500 my($uri, $orig_uri) = @_;
501 return qq|<a href="$uri">$orig_uri</a>|;
502 });
503 $finder->find(\$text, \&escapeHTML);
504 print "<pre>$text</pre>";
505  
506 =cut
507  
508  
509 402 ▶sub _is_uri {
Subroutine _is_uri: called CC=9 SCAR=22.0
510 402 ▶ @_ == 2 || __PACKAGE__->badinvo;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or __PACKAGE__->badinvo 402 0 402
511 402 my($self, $r_uri_cand) = @_;
512  
513 402 my $uri = $$r_uri_cand;
514  
515 # Translate schemeless to schemed if necessary.
516 402 ▶ $uri = $self->schemeless_to_schemed($uri) if
Branch: 1 branch
BranchTrueFalseTotal
if $uri =~ $self->schemeless_uri_re and not $uri =~ /^<?$schemeRe:/ 47 355 402
Condition66%2/3
$uri =~ $self->schemeless_uri_re and not $uri =~ /^<?$schemeRe:/
!ll&&!rl&&r
355047
MC/DC50%1/2
$uri =~ $self->schemeless_uri_re and not $uri =~ /^<?$schemeRe:/
$uri =~ $self->schemeless_uri_re not $uri =~ /^<?$schemeRe:/
Truth table66%2/3
A and not(B)
A= $uri =~ $self->schemeless_uri_reB= not $uri =~ /^<?$schemeRe:/
ABresult
0X0
100
111
517 $uri =~ $self->schemeless_uri_re and
518 $uri !~ /^<?$schemeRe:/;
519  
520 402 eval {
521 402 $uri = URI->new($uri);
522  
523 # Throw out anything with an invalid scheme.
524 402 ▶ my $has_invalid_scheme = $uri->isa("URI::_foreign") &&
Condition100%3/3
$uri->isa('URI::_foreign') && !($uri->scheme =~ /$extraSchemesRe/)
!ll&&!rl&&r
381129
MC/DC100%2/2
$uri->isa('URI::_foreign') && !($uri->scheme =~ /$extraSchemesRe/)
$uri->isa('URI::_foreign') !($uri->scheme =~ /$extraSchemesRe/)
Truth table100%3/3
A && not(B)
A= $uri->isa('URI::_foreign')B= !($uri->scheme =~ /$extraSchemesRe/)
ABresult
0X0
100
111
525 $uri->scheme !~ $extraSchemesRe;
526  
527 # Toss out things like http:// but keep file:///
528 402 my $is_empty = $uri =~ m{^$schemeRe://$};
529  
530 402 ▶ undef $uri if $has_invalid_scheme || $is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $has_invalid_scheme or $is_empty 15 387 402
Condition100%3/3
$has_invalid_scheme or $is_empty
l!l&&r!l&&!r
96387
MC/DC100%2/2
$has_invalid_scheme or $is_empty
$has_invalid_scheme $is_empty
Truth table100%3/3
A or B
A= $has_invalid_schemeB= $is_empty
ABresult
1X1
011
000
531 };
532  
533 402 ▶ if($@ || !defined $uri) { # leave everything untouched, its not a URI.
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $uri) { } 15 387 402
Condition66%2/3
$@ or not defined $uri
l!l&&r!l&&!r
015387
MC/DC50%1/2
$@ or not defined $uri
$@ not defined $uri
Truth table66%2/3
A or not(B)
A= $@B= not defined $uri
ABresult
1X1
011
000
534 15 return NO;
535 }
536 else { # Its a URI.
537 387 return $uri;
538 }
539 }
540  
541  
542 =head1 NOTES
543  
544 Will not find URLs with Internationalized Domain Names or pretty much
545 any non-ascii stuff in them. See
546 L<http://rt.cpan.org/Ticket/Display.html?id=44226>
547  
548  
549 =head1 AUTHOR
550  
551 Michael G Schwern <schwern@pobox.com> with insight from Uri Gutman,
552 Greg Bacon, Jeff Pinyan, Roderick Schertler and others.
553  
554 Roderick Schertler <roderick@argon.org> maintained versions 0.11 to 0.16.
555  
556 Darren Chamberlain wrote urifind.
557  
558  
559 =head1 LICENSE
560  
561 Copyright 2000, 2009-2010, 2014, 2016 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
562  
563 This program is free software; you can redistribute it and/or
564 modify it under the same terms as Perl itself.
565  
566 See F<http://www.perlfoundation.org/artistic_license_1_0>
567  
568 =head1 SEE ALSO
569  
570 L<urifind>, L<URI::Find::Schemeless>, L<URI>, RFC 3986 Appendix C
571  
572 =cut
573  
574 15 1;
↑ Summary lib/URI/Find/Schemeless.pm »