1File::Fetch(3) User Contributed Perl Documentation File::Fetch(3)
2
3
4
6 File::Fetch - A generic file fetching mechanism
7
9 use File::Fetch;
10
11 ### build a File::Fetch object ###
12 my $ff = File::Fetch->new(uri => 'http://some.where.com/dir/a.txt');
13
14 ### fetch the uri to cwd() ###
15 my $where = $ff->fetch() or die $ff->error;
16
17 ### fetch the uri to /tmp ###
18 my $where = $ff->fetch( to => '/tmp' );
19
20 ### parsed bits from the uri ###
21 $ff->uri;
22 $ff->scheme;
23 $ff->host;
24 $ff->path;
25 $ff->file;
26
28 File::Fetch is a generic file fetching mechanism.
29
30 It allows you to fetch any file pointed to by a "ftp", "http", "file",
31 "git" or "rsync" uri by a number of different means.
32
33 See the "HOW IT WORKS" section further down for details.
34
36 A "File::Fetch" object has the following accessors
37
38 $ff->uri
39 The uri you passed to the constructor
40
41 $ff->scheme
42 The scheme from the uri (like 'file', 'http', etc)
43
44 $ff->host
45 The hostname in the uri. Will be empty if host was originally
46 'localhost' for a 'file://' url.
47
48 $ff->vol
49 On operating systems with the concept of a volume the second
50 element of a file:// is considered to the be volume specification
51 for the file. Thus on Win32 this routine returns the volume, on
52 other operating systems this returns nothing.
53
54 On Windows this value may be empty if the uri is to a network
55 share, in which case the 'share' property will be defined.
56 Additionally, volume specifications that use '|' as ':' will be
57 converted on read to use ':'.
58
59 On VMS, which has a volume concept, this field will be empty
60 because VMS file specifications are converted to absolute UNIX
61 format and the volume information is transparently included.
62
63 $ff->share
64 On systems with the concept of a network share (currently only
65 Windows) returns the sharename from a file://// url. On other
66 operating systems returns empty.
67
68 $ff->path
69 The path from the uri, will be at least a single '/'.
70
71 $ff->file
72 The name of the remote file. For the local file name, the result of
73 $ff->output_file will be used.
74
75 $ff->file_default
76 The name of the default local file, that $ff->output_file falls
77 back to if it would otherwise return no filename. For example when
78 fetching a URI like http://www.abc.net.au/ the contents retrieved
79 may be from a remote file called 'index.html'. The default value of
80 this attribute is literally 'file_default'.
81
82 $ff->output_file
83 The name of the output file. This is the same as $ff->file, but any
84 query parameters are stripped off. For example:
85
86 http://example.com/index.html?x=y
87
88 would make the output file be "index.html" rather than
89 "index.html?x=y".
90
92 $ff = File::Fetch->new( uri => 'http://some.where.com/dir/file.txt' );
93 Parses the uri and creates a corresponding File::Fetch::Item object,
94 that is ready to be "fetch"ed and returns it.
95
96 Returns false on failure.
97
98 $where = $ff->fetch( [to => /my/output/dir/ | \$scalar] )
99 Fetches the file you requested and returns the full path to the file.
100
101 By default it writes to cwd(), but you can override that by specifying
102 the "to" argument:
103
104 ### file fetch to /tmp, full path to the file in $where
105 $where = $ff->fetch( to => '/tmp' );
106
107 ### file slurped into $scalar, full path to the file in $where
108 ### file is downloaded to a temp directory and cleaned up at exit time
109 $where = $ff->fetch( to => \$scalar );
110
111 Returns the full path to the downloaded file on success, and false on
112 failure.
113
114 $ff->error([BOOL])
115 Returns the last encountered error as string. Pass it a true value to
116 get the Carp::longmess() output instead.
117
119 File::Fetch is able to fetch a variety of uris, by using several
120 external programs and modules.
121
122 Below is a mapping of what utilities will be used in what order for
123 what schemes, if available:
124
125 file => LWP, lftp, file
126 http => LWP, HTTP::Tiny, wget, curl, lftp, fetch, HTTP::Lite, lynx, iosock
127 ftp => LWP, Net::FTP, wget, curl, lftp, fetch, ncftp, ftp
128 rsync => rsync
129 git => git
130
131 If you'd like to disable the use of one or more of these utilities
132 and/or modules, see the $BLACKLIST variable further down.
133
134 If a utility or module isn't available, it will be marked in a cache
135 (see the $METHOD_FAIL variable further down), so it will not be tried
136 again. The "fetch" method will only fail when all options are
137 exhausted, and it was not able to retrieve the file.
138
139 The "fetch" utility is available on FreeBSD. NetBSD and Dragonfly BSD
140 may also have it from "pkgsrc". We only check for "fetch" on those
141 three platforms.
142
143 "iosock" is a very limited IO::Socket::INET based mechanism for
144 retrieving "http" schemed urls. It doesn't follow redirects for
145 instance.
146
147 "git" only supports "git://" style urls.
148
149 A special note about fetching files from an ftp uri:
150
151 By default, all ftp connections are done in passive mode. To change
152 that, see the $FTP_PASSIVE variable further down.
153
154 Furthermore, ftp uris only support anonymous connections, so no named
155 user/password pair can be passed along.
156
157 "/bin/ftp" is blacklisted by default; see the $BLACKLIST variable
158 further down.
159
161 The behaviour of File::Fetch can be altered by changing the following
162 global variables:
163
164 $File::Fetch::FROM_EMAIL
165 This is the email address that will be sent as your anonymous ftp
166 password.
167
168 Default is "File-Fetch@example.com".
169
170 $File::Fetch::USER_AGENT
171 This is the useragent as "LWP" will report it.
172
173 Default is "File::Fetch/$VERSION".
174
175 $File::Fetch::FTP_PASSIVE
176 This variable controls whether the environment variable "FTP_PASSIVE"
177 and any passive switches to commandline tools will be set to true.
178
179 Default value is 1.
180
181 Note: When $FTP_PASSIVE is true, "ncftp" will not be used to fetch
182 files, since passive mode can only be set interactively for this binary
183
184 $File::Fetch::TIMEOUT
185 When set, controls the network timeout (counted in seconds).
186
187 Default value is 0.
188
189 $File::Fetch::WARN
190 This variable controls whether errors encountered internally by
191 "File::Fetch" should be "carp"'d or not.
192
193 Set to false to silence warnings. Inspect the output of the error()
194 method manually to see what went wrong.
195
196 Defaults to "true".
197
198 $File::Fetch::DEBUG
199 This enables debugging output when calling commandline utilities to
200 fetch files. This also enables "Carp::longmess" errors, instead of the
201 regular "carp" errors.
202
203 Good for tracking down why things don't work with your particular
204 setup.
205
206 Default is 0.
207
208 $File::Fetch::BLACKLIST
209 This is an array ref holding blacklisted modules/utilities for fetching
210 files with.
211
212 To disallow the use of, for example, "LWP" and "Net::FTP", you could
213 set $File::Fetch::BLACKLIST to:
214
215 $File::Fetch::BLACKLIST = [qw|lwp netftp|]
216
217 The default blacklist is [qw|ftp|], as "/bin/ftp" is rather unreliable.
218
219 See the note on "MAPPING" below.
220
221 $File::Fetch::METHOD_FAIL
222 This is a hashref registering what modules/utilities were known to fail
223 for fetching files (mostly because they weren't installed).
224
225 You can reset this cache by assigning an empty hashref to it, or
226 individually remove keys.
227
228 See the note on "MAPPING" below.
229
231 Here's a quick mapping for the utilities/modules, and their names for
232 the $BLACKLIST, $METHOD_FAIL and other internal functions.
233
234 LWP => lwp
235 HTTP::Lite => httplite
236 HTTP::Tiny => httptiny
237 Net::FTP => netftp
238 wget => wget
239 lynx => lynx
240 ncftp => ncftp
241 ftp => ftp
242 curl => curl
243 rsync => rsync
244 lftp => lftp
245 fetch => fetch
246 IO::Socket => iosock
247
249 So how do I use a proxy with File::Fetch?
250 "File::Fetch" currently only supports proxies with LWP::UserAgent. You
251 will need to set your environment variables accordingly. For example,
252 to use an ftp proxy:
253
254 $ENV{ftp_proxy} = 'foo.com';
255
256 Refer to the LWP::UserAgent manpage for more details.
257
258 I used 'lynx' to fetch a file, but its contents is all wrong!
259 "lynx" can only fetch remote files by dumping its contents to "STDOUT",
260 which we in turn capture. If that content is a 'custom' error file
261 (like, say, a "404 handler"), you will get that contents instead.
262
263 Sadly, "lynx" doesn't support any options to return a different exit
264 code on non-"200 OK" status, giving us no way to tell the difference
265 between a 'successful' fetch and a custom error page.
266
267 Therefor, we recommend to only use "lynx" as a last resort. This is why
268 it is at the back of our list of methods to try as well.
269
270 Files I'm trying to fetch have reserved characters or non-ASCII characters
271 in them. What do I do?
272 "File::Fetch" is relatively smart about things. When trying to write a
273 file to disk, it removes the "query parameters" (see the "output_file"
274 method for details) from the file name before creating it. In most
275 cases this suffices.
276
277 If you have any other characters you need to escape, please install the
278 "URI::Escape" module from CPAN, and pre-encode your URI before passing
279 it to "File::Fetch". You can read about the details of URIs and URI
280 encoding here:
281
282 <https://datatracker.ietf.org/doc/html/rfc2396>
283
285 Implement $PREFER_BIN
286 To indicate to rather use commandline tools than modules
287
289 Please report bugs or other issues to <bug-file-fetch@rt.cpan.org<gt>.
290
292 This module by Jos Boumans <kane@cpan.org>.
293
295 This library is free software; you may redistribute and/or modify it
296 under the same terms as Perl itself.
297
298
299
300perl v5.36.0 2023-01-20 File::Fetch(3)