jose
jose

Reputation: 1

Downloading a file from web using Perl CGI.pm

I am trying to write Perl script to download a file from web and then processing it. I have only CGI.pm installed in my server space. Please kindly advise.

Upvotes: 0

Views: 339

Answers (2)

Dave Cross
Dave Cross

Reputation: 69314

I think you're slightly confused. If you're trying to download a file, then CGI.pm will be no use to you. CGI.pm helps you to write CGI programs; it has nothing to help you download files.

To download a file from a web server, you need something that acts as a web client. A browser is a web client, but we need a program that acts like that - sending an HTTP request and processing the HTTP response.

The most obvious option is LWP::Simple. This includes a function called getstore().

my $code = getstore($url, $file);

You say that this isn't available to you. In order to write Perl programs of any complexity, you should find a place to host them that gives you access to lots of Perl modules (or allows you to install them). So much of modern Perl programming consists of joining together functions from CPAN modules and by not having access to that, you are seriously curtailing the options that are available to you.

But there are a couple of other options that are part of the standard Perl distribution and should be available to you.

HTTP::Tiny has been part of the core Perl distribution since Perl 5.14 in 2011. You could use its mirror() function to download your file.

File::Fetch has been part of the Perl code distribution since Perl 5.10 in 2007. You could use it like this:

use File::Fetch;

my $ff = File::Fetch->new(uri => $your_uri);
# $where will contain the path to the downloaded file
my $where = $ff->fetch or die $ff->error;

Upvotes: 1

Matthias Reissner
Matthias Reissner

Reputation: 365

A good starting point for you is to use LWP::Simple

Upvotes: 0

Related Questions