whilst performing file::fetch on a database of URLs, certain URLs contain only the top level domain such as http://google.com. How can i construct my if check such that it will verify that there is a file to fetched so when i create a new file::fetch object, it wouldnt throw me this error:
Use of uninitialized value $path in pattern match (m//) at C:/Perl/lib/File/Spec/Unix.pm line 267.
Use of uninitialized value in string eq at C:/Perl/lib/File/Fetch.pm line 395.
Here is a snippet of my code where i am trying to do some verification
my $uri_handle = File::Fetch->new(uri => $url);
my $getfile = $uri_handle ->file;
if ($getfile){
my $dir_handle = $uri_handle->fetch( to => $dir ) or die "Couldn't fetch file: $uri_handle->error\n";
#print "[ID:$myid] File fetch completed!\n\n";
}else{
print "[ID:$myid] There is no file to be fetched from $url\n\n";
}
But the file method fails too because it could not retrieve a file name at the URL.
Thread 1 terminated abnormally: Can't call method "file" on an undefined value at C:\test\multihashtest2.pl line 102.
Your perl script will not know what is on the webserver and what isn’t until it tries to load a URI — it doesn’t matter what the URL looks like. You’ll need to attempt to fetch the file and gracefully handle any errors.
It would help if you posted your code, but you can probably answer your own question by looking at the documentation.
In particular, the
new()methodreturns false on error, but that’s just validating the URI before it’s actually fetched. When you callfetch()on your File::Fetch object, itReturns the full path to the downloaded file on success, and false on failure.So handle what those methods return accordingly.
EDIT: now that you posted code. If the error is complaining that $uri_handle is undefined, check that $url is being set properly and check for errors. After line 1: