Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I download a file using Perl?

Tags:

download

perl

I'm running Perl on Windows XP, and I need to download a file from the URL http://marinetraffic2.aegean.gr/ais/getkml.aspx.

How should I do this? I have attempted using WWW::Mechanize, but I can't get my head around it.

This is the code I used:

my $url = 'marinetraffic2.aegean.gr/ais/getkml.aspx'; my $mech = WWW::Mechanize->new; $mech->get($url); 
like image 296
Sfairas Avatar asked Jan 12 '11 14:01

Sfairas


People also ask

How do I download a file in Perl?

use File::Fetch; my $url = 'http://www.example.com/file.txt'; my $ff = File::Fetch->new(uri => $url); my $file = $ff->fetch() or die $ff->error; Note that this module will in fact try to use LWP first if it is installed... Using File::Fetch under Linux 4.10.

How do I retrieve a file from a website in Perl?

Downloading a Web Page using the LWP::Simple Module LWP::Simple is a module in Perl which provides a get() that takes the URL as a parameter and returns the body of the document. It returns undef if the requested URL cannot be processed by the server.


2 Answers

I'd use LWP::Simple for this.

#!/usr/bin/perl  use strict; use warnings;  use LWP::Simple;  my $url = 'http://marinetraffic2.aegean.gr/ais/getkml.aspx'; my $file = 'data.kml';  getstore($url, $file); 
like image 71
Dave Cross Avatar answered Oct 18 '22 01:10

Dave Cross


I used File::Fetch as this is a core Perl module (I didn't need to install any additional packages) and will try a number of different ways to download a file depending on what's installed on the system.

use File::Fetch; my $url = 'http://www.example.com/file.txt'; my $ff = File::Fetch->new(uri => $url); my $file = $ff->fetch() or die $ff->error; 

Note that this module will in fact try to use LWP first if it is installed...

like image 28
maloo Avatar answered Oct 18 '22 03:10

maloo