man-pages/man3/LWP::Simple.3pm.html
2021-03-31 01:06:50 +01:00

325 lines
7.0 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of LWP::Simple</TITLE>
</HEAD><BODY>
<H1>LWP::Simple</H1>
Section: User Contributed Perl Documentation (3pm)<BR>Updated: 2019-11-29<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
LWP::Simple - simple procedural interface to LWP
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
perl -MLWP::Simple -e 'getprint &quot;<A HREF="http://www.sn.no">http://www.sn.no</A>&quot;'
use LWP::Simple;
$content = get(&quot;<A HREF="http://www.sn.no/">http://www.sn.no/</A>&quot;);
die &quot;Couldn't get it!&quot; unless defined $content;
if (mirror(&quot;<A HREF="http://www.sn.no/">http://www.sn.no/</A>&quot;, &quot;foo&quot;) == RC_NOT_MODIFIED) {
...
}
if (is_success(getprint(&quot;<A HREF="http://www.sn.no/">http://www.sn.no/</A>&quot;))) {
...
}
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
This module is meant for people who want a simplified view of the
libwww-perl library. It should also be suitable for one-liners. If
you need more control or access to the header fields in the requests
sent and responses received, then you should use the full object-oriented
interface provided by the LWP::UserAgent module.
<P>
The module will also export the LWP::UserAgent object as <TT>$ua</TT> if you
ask for it explicitly.
<P>
The user agent created by this module will identify itself as
<TT>&quot;LWP::Simple/#.##&quot;</TT>
and will initialize its proxy defaults from the environment (by
calling <TT>&quot;$ua-&gt;env_proxy&quot;</TT>).
<A NAME="lbAE">&nbsp;</A>
<H2>FUNCTIONS</H2>
The following functions are provided (and exported) by this module:
<A NAME="lbAF">&nbsp;</A>
<H3>get</H3>
<PRE>
my $res = get($url);
</PRE>
<P>
The <B>get()</B> function will fetch the document identified by the given <FONT SIZE="-1">URL</FONT>
and return it. It returns <TT>&quot;undef&quot;</TT> if it fails. The <TT>$url</TT> argument can
be either a string or a reference to a <FONT SIZE="-1">URI</FONT> object.
<P>
You will not be able to examine the response code or response headers
(like <TT>&quot;Content-Type&quot;</TT>) when you are accessing the web using this
function. If you need that information you should use the full <FONT SIZE="-1">OO</FONT>
interface (see LWP::UserAgent).
<A NAME="lbAG">&nbsp;</A>
<H3>head</H3>
<PRE>
my $res = head($url);
</PRE>
<P>
Get document headers. Returns the following 5 values if successful:
($content_type, <TT>$document_length</TT>, <TT>$modified_time</TT>, <TT>$expires</TT>, <TT>$server</TT>)
<P>
Returns an empty list if it fails. In scalar context returns <FONT SIZE="-1">TRUE</FONT> if
successful.
<A NAME="lbAH">&nbsp;</A>
<H3>getprint</H3>
<PRE>
my $code = getprint($url);
</PRE>
<P>
Get and print a document identified by a <FONT SIZE="-1">URL.</FONT> The document is printed
to the selected default filehandle for output (normally <FONT SIZE="-1">STDOUT</FONT>) as
data is received from the network. If the request fails, then the
status code and message are printed on <FONT SIZE="-1">STDERR.</FONT> The return value is
the <FONT SIZE="-1">HTTP</FONT> response code.
<A NAME="lbAI">&nbsp;</A>
<H3>getstore</H3>
<PRE>
my $code = getstore($url, $file)
</PRE>
<P>
Gets a document identified by a <FONT SIZE="-1">URL</FONT> and stores it in the file. The
return value is the <FONT SIZE="-1">HTTP</FONT> response code.
<A NAME="lbAJ">&nbsp;</A>
<H3>mirror</H3>
<PRE>
my $code = mirror($url, $file);
</PRE>
<P>
Get and store a document identified by a <FONT SIZE="-1">URL,</FONT> using
<I>If-modified-since</I>, and checking the <I>Content-Length</I>. Returns
the <FONT SIZE="-1">HTTP</FONT> response code.
<A NAME="lbAK">&nbsp;</A>
<H2>STATUS CONSTANTS</H2>
This module also exports the HTTP::Status constants and procedures.
You can use them when you check the response code from ``getprint'' in LWP::Simple,
``getstore'' in LWP::Simple or ``mirror'' in LWP::Simple. The constants are:
<P>
<PRE>
RC_CONTINUE
RC_SWITCHING_PROTOCOLS
RC_OK
RC_CREATED
RC_ACCEPTED
RC_NON_AUTHORITATIVE_INFORMATION
RC_NO_CONTENT
RC_RESET_CONTENT
RC_PARTIAL_CONTENT
RC_MULTIPLE_CHOICES
RC_MOVED_PERMANENTLY
RC_MOVED_TEMPORARILY
RC_SEE_OTHER
RC_NOT_MODIFIED
RC_USE_PROXY
RC_BAD_REQUEST
RC_UNAUTHORIZED
RC_PAYMENT_REQUIRED
RC_FORBIDDEN
RC_NOT_FOUND
RC_METHOD_NOT_ALLOWED
RC_NOT_ACCEPTABLE
RC_PROXY_AUTHENTICATION_REQUIRED
RC_REQUEST_TIMEOUT
RC_CONFLICT
RC_GONE
RC_LENGTH_REQUIRED
RC_PRECONDITION_FAILED
RC_REQUEST_ENTITY_TOO_LARGE
RC_REQUEST_URI_TOO_LARGE
RC_UNSUPPORTED_MEDIA_TYPE
RC_INTERNAL_SERVER_ERROR
RC_NOT_IMPLEMENTED
RC_BAD_GATEWAY
RC_SERVICE_UNAVAILABLE
RC_GATEWAY_TIMEOUT
RC_HTTP_VERSION_NOT_SUPPORTED
</PRE>
<A NAME="lbAL">&nbsp;</A>
<H2>CLASSIFICATION FUNCTIONS</H2>
The HTTP::Status classification functions are:
<A NAME="lbAM">&nbsp;</A>
<H3>is_success</H3>
<PRE>
my $bool = is_success($rc);
</PRE>
<P>
True if response code indicated a successful request.
<A NAME="lbAN">&nbsp;</A>
<H3>is_error</H3>
<PRE>
my $bool = is_error($rc)
</PRE>
<P>
True if response code indicated that an error occurred.
<A NAME="lbAO">&nbsp;</A>
<H2>CAVEAT</H2>
Note that if you are using both LWP::Simple and the very popular <FONT SIZE="-1">CGI</FONT>
module, you may be importing a <TT>&quot;head&quot;</TT> function from each module,
producing a warning like <TT>&quot;Prototype mismatch: sub main::head ($) vs none&quot;</TT>.
Get around this problem by just not importing LWP::Simple's
<TT>&quot;head&quot;</TT> function, like so:
<P>
<PRE>
use LWP::Simple qw(!head);
use CGI qw(:standard); # then only CGI.pm defines a head()
</PRE>
<P>
Then if you do need LWP::Simple's <TT>&quot;head&quot;</TT> function, you can just call
it as <TT>&quot;LWP::Simple::head($url)&quot;</TT>.
<A NAME="lbAP">&nbsp;</A>
<H2>SEE ALSO</H2>
<FONT SIZE="-1">LWP</FONT>, lwpcook, LWP::UserAgent, HTTP::Status, lwp-request,
lwp-mirror
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="4"><A HREF="#lbAE">FUNCTIONS</A><DD>
<DL>
<DT id="5"><A HREF="#lbAF">get</A><DD>
<DT id="6"><A HREF="#lbAG">head</A><DD>
<DT id="7"><A HREF="#lbAH">getprint</A><DD>
<DT id="8"><A HREF="#lbAI">getstore</A><DD>
<DT id="9"><A HREF="#lbAJ">mirror</A><DD>
</DL>
<DT id="10"><A HREF="#lbAK">STATUS CONSTANTS</A><DD>
<DT id="11"><A HREF="#lbAL">CLASSIFICATION FUNCTIONS</A><DD>
<DL>
<DT id="12"><A HREF="#lbAM">is_success</A><DD>
<DT id="13"><A HREF="#lbAN">is_error</A><DD>
</DL>
<DT id="14"><A HREF="#lbAO">CAVEAT</A><DD>
<DT id="15"><A HREF="#lbAP">SEE ALSO</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:47 GMT, March 31, 2021
</BODY>
</HTML>