📄 lwptut.3
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings. \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote. \*(C+ will.\" give a nicer C++. Capital omega is used to do unbreakable dashes and.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\. ds -- \(*W-. ds PI pi. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch. ds L" "". ds R" "". ds C` "". ds C' ""'br\}.el\{\. ds -- \|\(em\|. ds PI \(*p. ds L" ``. ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear. Run. Save yourself. No user-serviceable parts.. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds /.\}.if t \{\. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u". ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}. \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E. \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'. \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\. ds : e. ds 8 ss. ds o a. ds d- d\h'-1'\(ga. ds D- D\h'-1'\(hy. ds th \o'bp'. ds Th \o'LP'. ds ae ae. ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "lwptut 3".TH lwptut 3 "2004-11-30" "perl v5.10.0" "User Contributed Perl Documentation".\" For nroff, turn off justification. Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"lwptut \-\- An LWP Tutorial.SH "DESCRIPTION".IX Header "DESCRIPTION"\&\s-1LWP\s0 (short for \*(L"Library for \s-1WWW\s0 in Perl\*(R") is a very popular group ofPerl modules for accessing data on the Web. Like most Perlmodule-distributions, each of \s-1LWP\s0's component modules comes withdocumentation that is a complete reference to its interface. However,there are so many modules in \s-1LWP\s0 that it's hard to know where to startlooking for information on how to do even the simplest most commonthings..PPReally introducing you to using \s-1LWP\s0 would require a whole book \*(-- a bookthat just happens to exist, called \fIPerl & \s-1LWP\s0\fR. But this articleshould give you a taste of how you can go about some common tasks with\&\s-1LWP\s0..Sh "Getting documents with LWP::Simple".IX Subsection "Getting documents with LWP::Simple"If you just want to get what's at a particular \s-1URL\s0, the simplest wayto do it is LWP::Simple's functions..PPIn a Perl program, you can call its \f(CW\*(C`get($url)\*(C'\fR function. It will trygetting that \s-1URL\s0's content. If it works, then it'll return thecontent; but if there's some error, it'll return undef..PP.Vb 2\& my $url = \*(Aqhttp://freshair.npr.org/dayFA.cfm?todayDate=current\*(Aq;\& # Just an example: the URL for the most recent /Fresh Air/ show\&\& use LWP::Simple;\& my $content = get $url;\& die "Couldn\*(Aqt get $url" unless defined $content;\&\& # Then go do things with $content, like this:\&\& if($content =~ m/jazz/i) {\& print "They\*(Aqre talking about jazz today on Fresh Air!\en";\& }\& else {\& print "Fresh Air is apparently jazzless today.\en";\& }.Ve.PPThe handiest variant on \f(CW\*(C`get\*(C'\fR is \f(CW\*(C`getprint\*(C'\fR, which is useful in Perlone-liners. If it can get the page whose \s-1URL\s0 you provide, it sends itto \s-1STDOUT\s0; otherwise it complains to \s-1STDERR\s0..PP.Vb 1\& % perl \-MLWP::Simple \-e "getprint \*(Aqhttp://cpan.org/RECENT\*(Aq".Ve.PPThat is the \s-1URL\s0 of a plaintext file that lists new files in \s-1CPAN\s0 inthe past two weeks. You can easily make it part of a tidy littleshell command, like this one that mails you the list of new\&\f(CW\*(C`Acme::\*(C'\fR modules:.PP.Vb 2\& % perl \-MLWP::Simple \-e "getprint \*(Aqhttp://cpan.org/RECENT\*(Aq" \e\& | grep "/by\-module/Acme" | mail \-s "New Acme modules! Joy!" $USER.Ve.PPThere are other useful functions in LWP::Simple, including one functionfor running a \s-1HEAD\s0 request on a \s-1URL\s0 (useful for checking links, orgetting the last-revised time of a \s-1URL\s0), and two functions forsaving/mirroring a \s-1URL\s0 to a local file. See the LWP::Simpledocumentation for the full details, or chapter 2 of \fIPerl& \s-1LWP\s0\fR for more examples..Sh "The Basics of the \s-1LWP\s0 Class Model".IX Subsection "The Basics of the LWP Class Model"LWP::Simple's functions are handy for simple cases, but its functionsdon't support cookies or authorization, don't support setting headerlines in the \s-1HTTP\s0 request, generally don't support reading header linesin the \s-1HTTP\s0 response (notably the full \s-1HTTP\s0 error message, in case of anerror). To get at all those features, you'll have to use the full \s-1LWP\s0class model..PPWhile \s-1LWP\s0 consists of dozens of classes, the main two that you have tounderstand are LWP::UserAgent and HTTP::Response. LWP::UserAgentis a class for \*(L"virtual browsers\*(R" which you use for performing requests,and HTTP::Response is a class for the responses (or error messages)that you get back from those requests..PPThe basic idiom is \f(CW\*(C`$response = $browser\->get($url)\*(C'\fR, or more fullyillustrated:.PP.Vb 1\& # Early in your program:\& \& use LWP 5.64; # Loads all important LWP classes, and makes\& # sure your version is reasonably recent.\&\& my $browser = LWP::UserAgent\->new;\& \& ...\& \& # Then later, whenever you need to make a get request:\& my $url = \*(Aqhttp://freshair.npr.org/dayFA.cfm?todayDate=current\*(Aq;\& \& my $response = $browser\->get( $url );\& die "Can\*(Aqt get $url \-\- ", $response\->status_line\& unless $response\->is_success;\&\& die "Hey, I was expecting HTML, not ", $response\->content_type\& unless $response\->content_type eq \*(Aqtext/html\*(Aq;\& # or whatever content\-type you\*(Aqre equipped to deal with\&\& # Otherwise, process the content somehow:\& \& if($response\->decoded_content =~ m/jazz/i) {\& print "They\*(Aqre talking about jazz today on Fresh Air!\en";\& }\& else {\& print "Fresh Air is apparently jazzless today.\en";\& }.Ve.PPThere are two objects involved: \f(CW$browser\fR, which holds an object ofclass LWP::UserAgent, and then the \f(CW$response\fR object, which is ofclass HTTP::Response. You really need only one browser object perprogram; but every time you make a request, you get back a newHTTP::Response object, which will have some interesting attributes:.IP "\(bu" 4A status code indicatingsuccess or failure(which you can test with \f(CW\*(C`$response\->is_success\*(C'\fR)..IP "\(bu" 4An \s-1HTTP\s0 statusline that is hopefully informative if there's failure (which you cansee with \f(CW\*(C`$response\->status_line\*(C'\fR,returning something like \*(L"404 Not Found\*(R")..IP "\(bu" 4A \s-1MIME\s0 content-type like \*(L"text/html\*(R", \*(L"image/gif\*(R",\&\*(L"application/xml\*(R", etc., which you can see with \&\f(CW\*(C`$response\->content_type\*(C'\fR.IP "\(bu" 4The actual content of the response, in \f(CW\*(C`$response\->decoded_content\*(C'\fR.If the response is \s-1HTML\s0, that's where the \s-1HTML\s0 source will be; ifit's a \s-1GIF\s0, then \f(CW\*(C`$response\->decoded_content\*(C'\fR will be the binary\&\s-1GIF\s0 data..IP "\(bu" 4And dozens of other convenient and more specific methods that aredocumented in the docs for HTML::Response, and its superclassesHTML::Message and HTML::Headers..Sh "Adding Other \s-1HTTP\s0 Request Headers".IX Subsection "Adding Other HTTP Request Headers"The most commonly used syntax for requests is \f(CW\*(C`$response =$browser\->get($url)\*(C'\fR, but in truth, you can add extra \s-1HTTP\s0 headerlines to the request by adding a list of key-value pairs after the \s-1URL\s0,like so:.PP.Vb 1\& $response = $browser\->get( $url, $key1, $value1, $key2, $value2, ... );.Ve
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -