4 use vars qw($ua %loop_check $FULL_LWP @EXPORT @EXPORT_OK $VERSION);
8 @EXPORT = qw(get head getprint getstore mirror);
11 # I really hate this. I was a bad idea to do it in the first place.
12 # Wonder how to get rid of it??? (It even makes LWP::Simple 7% slower
15 push(@EXPORT, @HTTP::Status::EXPORT);
23 Exporter::export($pkg, $callpkg, @_);
26 use LWP::UserAgent ();
29 $ua = new LWP::UserAgent; # we create a global UserAgent object
30 $ua->agent("LWP::Simple/$VERSION ");
36 my $response = $ua->get(shift);
37 return $response->decoded_content if $response->is_success;
45 my $request = HTTP::Request->new(HEAD => $url);
46 my $response = $ua->request($request);
48 if ($response->is_success) {
49 return $response unless wantarray;
50 return (scalar $response->header('Content-Type'),
51 scalar $response->header('Content-Length'),
52 HTTP::Date::str2time($response->header('Last-Modified')),
53 HTTP::Date::str2time($response->header('Expires')),
54 scalar $response->header('Server'),
64 my $request = HTTP::Request->new(GET => $url);
65 local($\) = ""; # ensure standard $OUTPUT_RECORD_SEPARATOR
66 my $callback = sub { print $_[0] };
68 $callback = sub { $_[0] =~ s/\015?\012/\n/g; print $_[0] }
70 my $response = $ua->request($request, $callback);
71 unless ($response->is_success) {
72 print STDERR $response->status_line, " <URL:$url>\n";
81 my $request = HTTP::Request->new(GET => $url);
82 my $response = $ua->request($request, $file);
91 my $response = $ua->mirror($url, $file);
102 LWP::Simple - simple procedural interface to LWP
106 perl -MLWP::Simple -e 'getprint "http://www.sn.no"'
109 $content = get("http://www.sn.no/");
110 die "Couldn't get it!" unless defined $content;
112 if (mirror("http://www.sn.no/", "foo") == RC_NOT_MODIFIED) {
116 if (is_success(getprint("http://www.sn.no/"))) {
122 This module is meant for people who want a simplified view of the
123 libwww-perl library. It should also be suitable for one-liners. If
124 you need more control or access to the header fields in the requests
125 sent and responses received, then you should use the full object-oriented
126 interface provided by the C<LWP::UserAgent> module.
128 The following functions are provided (and exported) by this module:
134 The get() function will fetch the document identified by the given URL
135 and return it. It returns C<undef> if it fails. The $url argument can
136 be either a simple string or a reference to a URI object.
138 You will not be able to examine the response code or response headers
139 (like 'Content-Type') when you are accessing the web using this
140 function. If you need that information you should use the full OO
141 interface (see L<LWP::UserAgent>).
145 Get document headers. Returns the following 5 values if successful:
146 ($content_type, $document_length, $modified_time, $expires, $server)
148 Returns an empty list if it fails. In scalar context returns TRUE if
153 Get and print a document identified by a URL. The document is printed
154 to the selected default filehandle for output (normally STDOUT) as
155 data is received from the network. If the request fails, then the
156 status code and message are printed on STDERR. The return value is
157 the HTTP response code.
159 =item getstore($url, $file)
161 Gets a document identified by a URL and stores it in the file. The
162 return value is the HTTP response code.
164 =item mirror($url, $file)
166 Get and store a document identified by a URL, using
167 I<If-modified-since>, and checking the I<Content-Length>. Returns
168 the HTTP response code.
172 This module also exports the HTTP::Status constants and procedures.
173 You can use them when you check the response code from getprint(),
174 getstore() or mirror(). The constants are:
177 RC_SWITCHING_PROTOCOLS
181 RC_NON_AUTHORITATIVE_INFORMATION
196 RC_METHOD_NOT_ALLOWED
198 RC_PROXY_AUTHENTICATION_REQUIRED
203 RC_PRECONDITION_FAILED
204 RC_REQUEST_ENTITY_TOO_LARGE
205 RC_REQUEST_URI_TOO_LARGE
206 RC_UNSUPPORTED_MEDIA_TYPE
207 RC_INTERNAL_SERVER_ERROR
210 RC_SERVICE_UNAVAILABLE
212 RC_HTTP_VERSION_NOT_SUPPORTED
214 The HTTP::Status classification functions are:
218 =item is_success($rc)
220 True if response code indicated a successful request.
224 True if response code indicated that an error occurred.
228 The module will also export the LWP::UserAgent object as C<$ua> if you
229 ask for it explicitly.
231 The user agent created by this module will identify itself as
233 and will initialize its proxy defaults from the environment (by
234 calling $ua->env_proxy).
238 Note that if you are using both LWP::Simple and the very popular CGI.pm
239 module, you may be importing a C<head> function from each module,
240 producing a warning like "Prototype mismatch: sub main::head ($) vs
241 none". Get around this problem by just not importing LWP::Simple's
242 C<head> function, like so:
244 use LWP::Simple qw(!head);
245 use CGI qw(:standard); # then only CGI.pm defines a head()
247 Then if you do need LWP::Simple's C<head> function, you can just call
248 it as C<LWP::Simple::head($url)>.
252 L<LWP>, L<lwpcook>, L<LWP::UserAgent>, L<HTTP::Status>, L<lwp-request>,