The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

File::Fetch -- A generic file fetching mechanism

SYNOPSIS

    use File::Fetch;
    
    ### build a File::Fetch object ###
    my $ff = File::Fetch->new(uri => 'http://some.where.com/dir/a.txt');
    
    ### fetch the uri to cwd() ###
    my $where = $ff->fetch();
    
    ### fetch the uri to /tmp ###
    my $where = $ff->fetch( to => '/tmp' );
    
    ### parsed bits from the uri ###
    $ff->uri;
    $ff->scheme;
    $ff->host;
    $ff->path;
    $ff->file;
 

DESCRIPTION

File::Fetch is a generic file fetching mechanism.

It allows you to fetch any file pointed to by a ftp, http or file uri by a number of different means.

See the HOW IT WORKS section further down for details.

METHODS

$ff = File::Fetch->new( uri => 'http://some.where.com/dir/file.txt' );

Parses the uri and creates a corresponding File::Fetch::Item object, that is ready to be fetched and returns it.

Returns false on failure.

$ff->fetch( [to => /my/output/dir/] )

Fetches the file you requested. By default it writes to cwd(), but you can override that by specifying the to argument.

Returns the full path to the downloaded file on success, and false on failure.

ACCESSORS

A File::Fetch object has the following accessors

$ff->uri

The uri you passed to the constructor

$ff->scheme

The scheme from the uri (like 'file', 'http', etc)

$ff->host

The hostname in the uri, will be empty for a 'file' scheme.

$ff->path

The path from the uri, will be at least a single '/'.

$ff->file

The name of the remote file. Will be used as the name for the local file as well.

HOW IT WORKS

File::Fetch is able to fetch a variety of uris, by using several external programs and modules.

Below is a mapping of what utilities will be used in what order for what schemes, if available:

    file    => LWP
    http    => LWP, wget, curl, lynx
    ftp     => LWP, Net::FTP, wget, curl, ncftp, ftp

If you'd like to disable the use of one or more of these utilities and/or modules, see the $BLACKLIST variable further down.

If a utility or module isn't available, it will be marked in a cache (see the $METHOD_FAIL variable further down), so it will not be tried again. The fetch method will only fail when all options are exhausted, and it was not able to retrieve the file.

A special note about fetching files from an ftp uri:

By default, all ftp connections are done in passive mode. To change that, see the $FTP_PASSIVE variable further down.

Furthermore, ftp uris only support anonymous connections, so no named user/password pair can be passed along.

Also, /bin/ftp is rather unreliable, so it is blacklisted by default but you may enable it if you wish; see the $BLACKLIST variable further down.

GLOBAL VARIABLES

The behaviour of File::Fetch can be altered by changing the following global variables:

$File::Fetch::FROM_EMAIL

This is the email address that will be sent as your anonymous ftp password.

Default is File-Fetch@example.com.

$File::Fetch::USER_AGENT

This is the useragent as LWP will report it.

Default is File::Fetch/$VERSION.

$File::Fetch::FTP_PASSIVE

This variable controls whether the environment variable FTP_PASSIVE and any passive switches to commandline tools will be set to true.

Default value is 1.

Note: When $FTP_PASSIVE is true, ncftp will not be used to fetch files, since passive mode can only be set interactively for this binary

$File::Fetch::DEBUG

This enables debugging output when calling commandline utilities to fetch files.

Default is 0.

$File::Fetch::BLACKLIST

This is an array ref holding blacklisted modules/utilities for fetching files with.

To disallow the use of, for example, LWP and Net::FTP, you could set $File::Fetch::BLACKLIST to:

    $File::Fetch::BLACKLIST = [qw|lwp netftp|]
    

Default is a ['ftp'].

See the note on MAPPING below.

$File::Fetch::METHOD_FAIL

This is a hashref registering what modules/utilities were known to fail for fetching files (mostly because they weren't installed).

You can reset this cache by assigning an empty hashref to it, or individually remove keys.

See the note on MAPPING below.

MAPPING

Here's a quick mapping for the utilities/modules, and their names for the $BLACKLIST, $METHOD_FAIL and other internal functions.

    LWP         => lwp
    Net::FTP    => netftp
    wget        => wget
    lynx        => lynx
    ncftp       => ncftp
    ftp         => ftp
    curl        => curl

FREQUENTLY ASKED QUESTIONS

Why don't you just use File::Copy for file:// schemes?

It's just too much of a hassle to figure out what to do with volumes and paths on any OS that isn't a Unix. LWP however Does The Right Thing, so it's easier to use that. Besides, if you're just copying a file, you can probably do it yourself :)

So how do I use a proxy with File::Fetch?

File::Fetch currently only supports proxies with LWP::UserAgent. You will need to set your environment variables accordingly. For example, to use an ftp proxy:

    $ENV{ftp_proxy} = 'foo.com';

Refer to the LWP::UserAgent manpage for more details.

TODO

Implement $PREFER_BIN

To indicate to rather use commandline tools than modules

Implement rsync://

To allow copying of files over rsync

AUTHORS

This module by Jos Boumans <kane@cpan.org>.

COPYRIGHT

This module is copyright (c) 2003 Jos Boumans <kane@cpan.org>. All rights reserved.

This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 760:

You forgot a '=back' before '=head1'