FirstGoodURL
view release on metacpan or search on metacpan
FirstGoodURL.pm view on Meta::CPAN
package FirstGoodURL;
$VERSION = '1.11';
use LWP::UserAgent;
use Carp;
use strict;
my $status = { 200 => 1 };
my $ctype = {};
my $ua;
sub import { $ua ||= LWP::UserAgent->new }
sub with {
my $class = shift;
carp "no content-type or status given" if not @_;
$status = { 200 => 1 };
$ctype = {};
(/\D/ ? $ctype->{$_} : $status->{$_}) = $_ for @_;
return $class;
}
sub in {
shift;
my $match;
for (@_) {
my $req = $ua->request(HTTP::Request->new(HEAD => $_));
my ($rc,$rt) = ($req->code, $req->content_type);
if (not keys %$ctype) { $match = $_ and last if $status->{$rc} }
else { next if not $status->{$rc} }
if (keys %$ctype == 1 and my($regex) = (each %$ctype)[1]) {
$match = $_ and last if $rt =~ $regex;
}
else { for (keys %$ctype) { $match = $_ and last if $ctype->{$rt} } }
}
$status = { 200 => 1 };
$ctype = {};
return $match;
}
1;
__END__
=head1 NAME
FirstGoodURL - determines first successful URL in list
=head1 SYNOPSIS
use FirstGoodURL;
use strict;
my @URLs = (...);
my $match;
if ($match = FirstGoodURL->in(@URLs)) {
print "good URL: $match\n";
}
else {
print "no URL was alive\n";
}
if ($match = FirstGoodURL->with('image/png')->in(@URLs)) {
print "PNG found at $match\n";
}
( run in 1.121 second using v1.01-cache-2.11-cpan-524268b4103 )