File::Fetch(3pm)
NAME π
okay bestie, welcome to the File::Fetch(3pm) manual page β the whole NAME vibe is literally: File::Fetch β A generic file fetching mechanism. That's it, that's the energy β¨
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() or die $ff->error;
### 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 π¦
okay so File::Fetch is literally a generic file fetching mechanism. no cap.
It lets you fetch any file pointed to by a "ftp", "http", "file", "git" or "rsync" uri by a number of different means. the way it just handles all those schemes is giving main character energy, honestly π
See the HOW IT WORKS section further down for details π
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 if host was originally'localhost'for a'file://'url.$ff->volβ on operating systems with the concept of a volume, the second element of afile://is considered to the be volume specification for the file. thus on Win32 this routine returns the volume, on other operating systems this returns nothing.
On Windows this value may be empty if the uri is to a network share, in which case the 'share' property will be defined. Additionally, volume specifications that use '|' as ':' will be converted on read to use ':'.
On VMS, which has a volume concept, this field will be empty because VMS file specifications are converted to absolute UNIX format and the volume information is transparently included.
$ff->shareβ on systems with the concept of a network share (currently only Windows) returns the sharename from afile:////url. on other operating systems returns empty.$ff->pathβ the path from the uri, will be at least a single'/'.$ff->fileβ the name of the remote file. for the local file name, the result of$ff->output_filewill be used.$ff->file_defaultβ the name of the default local file, that$ff->output_filefalls back to if it would otherwise return no filename. for example when fetching a URI likehttp://www.abc.net.au/the contents retrieved may be from a remote file called'index.html'. the default value of this attribute is literally'file_default'.$ff->output_fileβ the name of the output file. this is the same as$ff->file, but any query parameters are stripped off. for example:http://example.com/index.html?x=ywould make the output file be"index.html"rather than"index.html?x=y".
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 "fetch"ed and returns it.
Returns false on failure.
$where = $ff->fetch( [to => /my/output/dir/ | \$scalar] )
Fetches the file you requested and returns the full path to the file.
By default it writes to cwd(), but you can override that by specifying the "to" argument:
### file fetch to /tmp, full path to the file in $where
$where = $ff->fetch( to => '/tmp' );
### file slurped into $scalar, full path to the file in $where
### file is downloaded to a temp directory and cleaned up at exit time
$where = $ff->fetch( to => \$scalar );
Returns the full path to the downloaded file on success, and false on failure.
$ff->error([BOOL])
Returns the last encountered error as string. pass it a true value to get the Carp::longmess() output instead.
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, lftp, file
http => LWP, HTTP::Tiny, wget, curl, lftp, fetch, HTTP::Lite, lynx, iosock
ftp => LWP, Net::FTP, wget, curl, lftp, fetch, ncftp, ftp
rsync => rsync
git => git
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.
The "fetch" utility is available on FreeBSD. NetBSD and Dragonfly BSD may also have it from "pkgsrc". We only check for "fetch" on those three platforms.
"iosock" is a very limited IO::Socket::INET based mechanism for retrieving "http" schemed urls. It doesn't follow redirects for instance.
"git" only supports "git://" style urls.
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.
/bin/ftp is blacklisted by default; 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 is1.
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::TIMEOUTβ when set, controls the network timeout (counted in seconds). default value is0.$File::Fetch::WARNβ this variable controls whether errors encountered internally by"File::Fetch"should be"carp"'d or not. set to false to silence warnings. inspect the output of theerror()method manually to see what went wrong. defaults to"true".$File::Fetch::DEBUGβ this enables debugging output when calling commandline utilities to fetch files. this also enables"Carp::longmess"errors, instead of the regular"carp"errors. good for tracking down why things don't work with your particular setup. default is0.$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::BLACKLISTto:
$File::Fetch::BLACKLIST = [qw|lwp netftp|]
The default blacklist is [qw|ftp|], as /bin/ftp is rather unreliable.
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
HTTP::Lite => httplite
HTTP::Tiny => httptiny
Net::FTP => netftp
wget => wget
lynx => lynx
ncftp => ncftp
ftp => ftp
curl => curl
rsync => rsync
lftp => lftp
fetch => fetch
IO::Socket => iosock
FREQUENTLY ASKED QUESTIONS π¬
So how do I use a proxy with File::Fetch? π«Ά
< purple>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.
I used 'lynx' to fetch a file, but its contents is all wrong! π
"lynx" can only fetch remote files by dumping its contents to "STDOUT", which we in turn capture. if that content is a 'custom' error file (like, say, a "404 handler"), you will get that contents instead.
Sadly, "lynx" doesn't support any options to return a different exit code on non-"200 OK" status, giving us no way to tell the difference between a 'successful' fetch and a custom error page.
Therefor, we recommend to only use "lynx" as a last resort. this is why it is at the back of our list of methods to try as well.
Files I'm trying to fetch have reserved characters or non-ASCII characters in them. What do I do? π§
"File::Fetch" is relatively smart about things. when trying to write a file to disk, it removes the "query parameters" (see the "output_file" method for details) from the file name before creating it. in most cases this suffices.
If you have any other characters you need to escape, please install the "URI::Escape" module from CPAN, and pre-encode your URI before passing it to "File::Fetch". you can read about the details of URIs and URI encoding here:
https://datatracker.ietf.org/doc/html/rfc2396
TODO π§
- Implement
$PREFER_BINβ to indicate to rather use commandline tools than modules.
BUG REPORTS π
Please report bugs or other issues to <bug-file-fetch@rt.cpan.org<gt>.
AUTHOR π
This module by Jos Boumans <kane@cpan.org>.
COPYRIGHT π
This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.
and that's the whole File::Fetch(3pm) page, bestie π
β¨ source/version is perl v5.42.3, date 2026-11-03 β now go fetch something iconic π«Άπ
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms