my $CACHE_DIR = '/tmp/xmlcache'; use File::Path qw/make_path/; make_path($CACHE_DIR, {verbose=>1}); use URI; use HTTP::Tiny; use Text::CleanFragment qw/clean_fragment/; use File::Spec::Functions qw/catfile/; my $http = HTTP::Tiny->new; XML::LibXML::externalEntityLoader(sub { my ($url, $id) = @_; die "Can't handle ID '$id'" if length $id; my $uri = URI->new($url); my $file; if (!$uri->scheme) { $file = $url } elsif ($uri->scheme eq 'file') { $file = $uri->path } elsif ($uri->scheme=~/\Ahttps?\z/i) { # Note there is a (tiny) chance of filename collisions here! $file = catfile($CACHE_DIR, clean_fragment("$uri")); if (!-e $file) { warn "'$uri' => Mirroring to '$file'...\n"; #Debug my $resp = $http->mirror($uri, "$file"); die "$uri: $resp->{status} $resp->{reason}\n" unless $resp->{success}; } } else { die "Can't handle URL scheme: ".$uri->scheme } warn "'$uri' => Loading '$file' from disk\n"; #Debug open my $fh, '<', $file or die "$file: $!"; my $data = do { local $/; <$fh> }; close $fh; return $data; });