Monday, February 8, 2016

Perl: Download file from URL


Abstract: implement the Perl package LWP::Simple to download files.


The below subroutine download_file() is used for download file from mirBase.

use LWP::Simple;

#-- fetch file
sub download_file{
my($url, $file_name, $local_dir)=@_;
$url .= '/' unless $url=~/\/$/;
$local_dir .= '/' unless $local_dir=~/\/$/;
#fetch file
my $url_file= $url.$file_name;
my $local_file=$local_dir.$file_name;
my $status = getstore($url_file, $local_file);
#report status
if ( is_success($status) ){
print "downloaded from $url and save it as $local_file\n";
#unzip file
system("gunzip $local_file") if $local_file=~/\.gz$/;
}
else{
print "error downloading file: $status\n";
}
#return
}

#main program

download_file("ftp://mirbase.org/pub/mirbase/CURRENT/", "hairpin.fa.gz", "/home/yuan/Downloads/");
print "ok\n";

No comments:

Post a Comment