10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 5 ■ LWP MODULES 95When called in a list context, the head() function returns five items from the response header:• Content type• Document length• Modified time• Expires• ServerFor example, the head() function might be called in this manner in order <strong>to</strong> capture thefive values:($content_type,$doclen,$modified,$expires,$server) = \head('http://www.braingia.org');The Mirror FunctionThe mirror() function works in much the same was as the gets<strong>to</strong>re() function, but alsoincludes a check <strong>to</strong> compare the modification time of the local file and the modificationtime of the remote resource, based on the If-Modified-Since response header. Listing 5-3shows an example of the mirror() function in action (Example3.pl):Listing 5-3. Using the mirror() Function#!/usr/bin/perl -wuse LWP::Simple;use strict;my $url = "http://www.braingia.org/";my $file = "/tmp/braingiamirrorweb";my $status = mirror($url,$file);die "Cannot retrieve $url" unless is_success($status);This program won’t produce any output <strong>to</strong> the terminal unless there is an error. If it’s successful,there will be a file in /tmp called braingiamirrorweb. Inside that file will be raw outputsuch as HTML and other bits as found on the web page. The contents will be similar <strong>to</strong> thefollowing:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!