I'm trying to create a hash of a file that i have downloaded using the module File::Fetch. I'm trying to store the downloaded file name and its hash in the database as a record. However, none of the methods of File::Fetch objects provide the downloaded file. Is there any way to download the file into a variable in order to create a hash of it?
Here is a snippet of my code,
eval{
$uri_handle = File::Fetch->new(uri => $nurl) or die "Could'nt create fetch object\n";
$getfile = $uri_handle ->output_file or die "There is no file to be fetched\n";
my $dir_handle = $uri_handle->fetch( to => $dir2 ) or die "Couldn't fetch file: $uri_handle->error\n";
# print "$getfile was successfully saved to $dir\n";
};
if ($@){
print "\n There is no file at $url\n\n";
}else{
my $file_data_handle = DBI->connect($database_connection_string,$database_user,$database_pass) or die "Couldn't open database: $DBI::errstr\n";
my $file_statement_handle = $file_data_handle->prepare('insert into files (filename,filehash,sourceurl,originalurl) VALUES ($getfile,$filehash,$nurl,$url)') or die "Couldn't prepare statement: $DBI::errstr\n";
$file_statement_handle->execute() or die "Couldn't execute statement: $DBI::errstr\n";
$file_data_handle->disconnect();
}
if ($@)...for error detection but the result ofevalitself. A common idiom is something likeeval { ...; 1; } or do { print "Exception occured: $@\n"; };$ff->file The name of the remote file. For the local file name, the result of $ff->output_file will be used.$dir2$url_handleshould hold that file's name, so just use that name, open the local file and read its content.