1File::Fetch(3pm) Perl Programmers Reference Guide File::Fetch(3pm)
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 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->output_file
76 The name of the output file. This is the same as $ff->file, but any
77 query parameters are stripped off. For example:
78
79 http://example.com/index.html?x=y
80
81 would make the output file be "index.html" rather than
82 "index.html?x=y".
83
85 $ff = File::Fetch->new( uri => 'http://some.where.com/dir/file.txt' );
86 Parses the uri and creates a corresponding File::Fetch::Item object,
87 that is ready to be "fetch"ed and returns it.
88
89 Returns false on failure.
90
91 $where = $ff->fetch( [to => /my/output/dir/ | \$scalar] )
92 Fetches the file you requested and returns the full path to the file.
93
94 By default it writes to "cwd()", but you can override that by
95 specifying the "to" argument:
96
97 ### file fetch to /tmp, full path to the file in $where
98 $where = $ff->fetch( to => '/tmp' );
99
100 ### file slurped into $scalar, full path to the file in $where
101 ### file is downloaded to a temp directory and cleaned up at exit time
102 $where = $ff->fetch( to => \$scalar );
103
104 Returns the full path to the downloaded file on success, and false on
105 failure.
106
107 $ff->error([BOOL])
108 Returns the last encountered error as string. Pass it a true value to
109 get the "Carp::longmess()" output instead.
110
112 File::Fetch is able to fetch a variety of uris, by using several
113 external programs and modules.
114
115 Below is a mapping of what utilities will be used in what order for
116 what schemes, if available:
117
118 file => LWP, lftp, file
119 http => LWP, wget, curl, lftp, lynx, iosock
120 ftp => LWP, Net::FTP, wget, curl, lftp, ncftp, ftp
121 rsync => rsync
122
123 If you'd like to disable the use of one or more of these utilities
124 and/or modules, see the $BLACKLIST variable further down.
125
126 If a utility or module isn't available, it will be marked in a cache
127 (see the $METHOD_FAIL variable further down), so it will not be tried
128 again. The "fetch" method will only fail when all options are
129 exhausted, and it was not able to retrieve the file.
130
131 "iosock" is a very limited IO::Socket::INET based mechanism for
132 retrieving "http" schemed urls. It doesn't follow redirects for
133 instance.
134
135 A special note about fetching files from an ftp uri:
136
137 By default, all ftp connections are done in passive mode. To change
138 that, see the $FTP_PASSIVE variable further down.
139
140 Furthermore, ftp uris only support anonymous connections, so no named
141 user/password pair can be passed along.
142
143 "/bin/ftp" is blacklisted by default; see the $BLACKLIST variable
144 further down.
145
147 The behaviour of File::Fetch can be altered by changing the following
148 global variables:
149
150 $File::Fetch::FROM_EMAIL
151 This is the email address that will be sent as your anonymous ftp
152 password.
153
154 Default is "File-Fetch@example.com".
155
156 $File::Fetch::USER_AGENT
157 This is the useragent as "LWP" will report it.
158
159 Default is "File::Fetch/$VERSION".
160
161 $File::Fetch::FTP_PASSIVE
162 This variable controls whether the environment variable "FTP_PASSIVE"
163 and any passive switches to commandline tools will be set to true.
164
165 Default value is 1.
166
167 Note: When $FTP_PASSIVE is true, "ncftp" will not be used to fetch
168 files, since passive mode can only be set interactively for this binary
169
170 $File::Fetch::TIMEOUT
171 When set, controls the network timeout (counted in seconds).
172
173 Default value is 0.
174
175 $File::Fetch::WARN
176 This variable controls whether errors encountered internally by
177 "File::Fetch" should be "carp"'d or not.
178
179 Set to false to silence warnings. Inspect the output of the "error()"
180 method manually to see what went wrong.
181
182 Defaults to "true".
183
184 $File::Fetch::DEBUG
185 This enables debugging output when calling commandline utilities to
186 fetch files. This also enables "Carp::longmess" errors, instead of the
187 regular "carp" errors.
188
189 Good for tracking down why things don't work with your particular
190 setup.
191
192 Default is 0.
193
194 $File::Fetch::BLACKLIST
195 This is an array ref holding blacklisted modules/utilities for fetching
196 files with.
197
198 To disallow the use of, for example, "LWP" and "Net::FTP", you could
199 set $File::Fetch::BLACKLIST to:
200
201 $File::Fetch::BLACKLIST = [qw|lwp netftp|]
202
203 The default blacklist is [qw|ftp|], as "/bin/ftp" is rather unreliable.
204
205 See the note on "MAPPING" below.
206
207 $File::Fetch::METHOD_FAIL
208 This is a hashref registering what modules/utilities were known to fail
209 for fetching files (mostly because they weren't installed).
210
211 You can reset this cache by assigning an empty hashref to it, or
212 individually remove keys.
213
214 See the note on "MAPPING" below.
215
217 Here's a quick mapping for the utilities/modules, and their names for
218 the $BLACKLIST, $METHOD_FAIL and other internal functions.
219
220 LWP => lwp
221 Net::FTP => netftp
222 wget => wget
223 lynx => lynx
224 ncftp => ncftp
225 ftp => ftp
226 curl => curl
227 rsync => rsync
228 lftp => lftp
229 IO::Socket => iosock
230
232 So how do I use a proxy with File::Fetch?
233 "File::Fetch" currently only supports proxies with LWP::UserAgent. You
234 will need to set your environment variables accordingly. For example,
235 to use an ftp proxy:
236
237 $ENV{ftp_proxy} = 'foo.com';
238
239 Refer to the LWP::UserAgent manpage for more details.
240
241 I used 'lynx' to fetch a file, but its contents is all wrong!
242 "lynx" can only fetch remote files by dumping its contents to "STDOUT",
243 which we in turn capture. If that content is a 'custom' error file
244 (like, say, a "404 handler"), you will get that contents instead.
245
246 Sadly, "lynx" doesn't support any options to return a different exit
247 code on non-"200 OK" status, giving us no way to tell the difference
248 between a 'successfull' fetch and a custom error page.
249
250 Therefor, we recommend to only use "lynx" as a last resort. This is why
251 it is at the back of our list of methods to try as well.
252
253 Files I'm trying to fetch have reserved characters or non-ASCII characters
254 in them. What do I do?
255 "File::Fetch" is relatively smart about things. When trying to write a
256 file to disk, it removes the "query parameters" (see the "output_file"
257 method for details) from the file name before creating it. In most
258 cases this suffices.
259
260 If you have any other characters you need to escape, please install the
261 "URI::Escape" module from CPAN, and pre-encode your URI before passing
262 it to "File::Fetch". You can read about the details of URIs and URI
263 encoding here:
264
265 http://www.faqs.org/rfcs/rfc2396.html
266
268 Implement $PREFER_BIN
269 To indicate to rather use commandline tools than modules
270
272 Please report bugs or other issues to <bug-file-fetch@rt.cpan.org<gt>.
273
275 This module by Jos Boumans <kane@cpan.org>.
276
278 This library is free software; you may redistribute and/or modify it
279 under the same terms as Perl itself.
280
281
282
283perl v5.12.4 2011-06-07 File::Fetch(3pm)