Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Presentation: Intro to Perl and LWP

by hossman (Prior)
on Mar 02, 2005 at 00:10 UTC ( #435663=perlmeditation: print w/replies, xml ) Need Help??

I few weeks ago my boss asked me to give a short presentation on Perl and LWP to a bunch of our developers. The audience was mainly Java/JSP folks with minimal perl exposure, and the goal of the presentation was to make them aware of some of the "quick wins" that they could achieve by writting little test scripts and utilities in Perl

I figured these slides might come in handy for someone else down the road, so I decided to post them here.

NOTE: I'm the kind of person who likes my slides to be very basic, and just display the stuff that you don't want to have to convey oraly. The majority of the information from my talk was verbal; but anyone with a good background on LWP should be able to use these slides as a starting point to give the exact same talk without much preperation.

Perl & LWP HTTP Connections In Perl

What is HTTP? A simple to read (and write) protocol... lynx -source -mime_header telnet 80 GET /bogus HTTP/1.0 Host: $ telnet 80 Trying Connected to Escape character is '^]'. GET /bogus HTTP/1.0 Host: HTTP/1.1 404 Not Found Date: Fri, 04 Feb 2005 00:43:02 GMT Server: Apache/2.0 Accept-Ranges: bytes P3P: CP="CAO DSP COR CURa ADMa DEVa PSAa PSDa IVAi IVDi CONi OUR OTRi +IND PHY ONL UNI FIN COM NAV INT DEM STA" Connection: close Content-Type: text/html Expires: Fri, 04 Feb 2005 00:43:02 GMT ... <html>

LWP is easy to use #!/usr/local/bin/perl use strict; use warnings; use LWP::UserAgent; use HTTP::Request; my $url = ''; my $ua = LWP::UserAgent->new; my $request = HTTP::Request->new(GET => $url); my $response = $ua->request($request); if ($response->is_success) { print $response->content; } else { print $response->status_line, " <URL:$url>\n"; }

LWP::Simple is even easier #!/usr/local/bin/perl use strict; use warnings; use LWP::Simple; getprint('');

First, a Perl refresher #!/user/local/bin/perl # Tell your OS this is a perl script use strict; # If you write a script without these use warnings; # ... two lines, you owe me 10 buck +s use Module::Name; # "use" is like java's "import" my $stuff = undef; # "my" is how you declare variables # ... "undef" is like java's "null" my $bat = get_bat($name); # Function calls are just like java my $bar = ['Hoss',2,3,$stuff]; # square brackets make an array ref my $baz = {a => 1, b => 2}; # currly braces make a hash (ie: Map) + ref my $first = $bar->[0]; # -> dereferences attributes/methods $baz->{'c'} = 3; # ... like "." in java. $bat->do_something(); # if ($bar->[1]) { # "if" and "print" work the way you t +hink print "$first is true\n"; # Double quotes evaluate variables } # ... prints: Hoss is true

More fun with LWP::Simple #!/usr/local/bin/perl use strict; use warnings; use LWP::Simple; my $data = get(''); if (defined $data) { print $data; } if (is_success(getstore('', './mysimon.html'))) { print "fetched mysimon front door just fine\n"; } if (RC_NOT_MODIFIED != mirror('','./mp3.html')) { print "Something interesting happened with\n"; } my ($type, $length, $mod, $exp, $server) = head(''); print "Content Type is $type, and the Server is $server\n";

LWP::UserAgent is more "full featured" #!/usr/local/bin/perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new(); $ua->cookie_jar({}); $ua->agent('Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7)'); $ua->timeout(42); # seconds $ua->proxy('http', ''); $ua->no_proxy(''); my $response1 = $ua->get(''); my $response2 = $ua->head(''); my $response3 = $ua->post('', {'descr' => 'your bug description', 'component' => 'your bug component', 'priority' => 'P1' });

HTTP::Request & HTTP::Response #!/usr/local/bin/perl use strict; use warnings; use LWP::UserAgent; use HTTP::Request; use HTTP::Response; my ($ua, $res) = (LWP::UserAgent->new(), undef); # these two lines are equivilent $res = $ua->get(''); $res = $ua->request(HTTP::Request->new('GET','')); # but you can do a lot more with the Request object... my $req = HTTP::Request->new('GET',''); $req->header(Accept => 'text/x-dvi, text/html, text/*, */*'); $res = $ua->request($req); # the Response object also contains a lot of usefull info... print "The Status code was: ", $res->code(), "\n"; print "The Content Type was: ", $res->header('Content-Type'), "\n"; print "The URL was: ", $res->request()->uri(), "\n"; while ($res = $res->previous()) { print "Redirected from: ", $res->request()->uri(), "\n"; }

Previous script's output $ ./ The Status code was: 200 The Content Type was: text/html The URL was: Redirected from: Redirected from:

Other cool features worth looking at * Use proxies * POST complex requests (including file uploads) * Authenticate using Basic Credentials * Read/write cookies from disk (in Mozilla or Microsoft formats) * Parse/Submit forms auto-magically * LWP:RobotUA, LWP::Parallel::UserAgent, LWP::UserAgent::FramesReady

One caveat to LWP::Simple #!/usr/local/bin/perl use strict; use warnings; # use LWP::Simple this way, to ensure a proper LWP::UserAgent object # (that speaks HTTP/1.1) is used by all methods. # # Without it some methods use a lightweight client that only # speaks HTTP/1.0, which your server might treat differently, # and might give you different results then you expect. use LWP::Simple qw(:DEFAULT $ua); my $data = get(''); #

More Info perldoc LWP perldoc LWP::Simple perldoc LWP::UserAgent perldoc HTTP::Request::Common perldoc lwpcook

Replies are listed 'Best First'.
Re: Presentation: Intro to Perl and LWP
by machinecraig (Monk) on Mar 02, 2005 at 05:06 UTC
    ++! I thought this was an excellent intro to making productive use of LWP; you did a good job of presenting an ultra-quick run through of perl as well - I laughed out loud when I read your comment re: use strict + warnings.

    Thanks for the inspiring read hossman!
Re: Presentation: Intro to Perl and LWP
by crypix (Acolyte) on Mar 04, 2005 at 03:37 UTC
    I think this is a terrific intro for Perl/LWP. Well done if I might say.. ~crypix
      Is a intro for who never saw Perl! And I can tell you that for the Delphi and Java folks Perl is very strange!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://435663]
Approved by davidj
Front-paged by ybiC
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2019-08-17 21:34 GMT
Find Nodes?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?

    Results (134 votes). Check out past polls.