Home / Archive by category 'Perl'

Perl

Using LWP and HTML::FormatText to retrieve text from a page in Perl

!/usr/bin/perl -w

use strict;
use HTML::TreeBuilder;
use HTML::FormatText;
use LWP::Simple;

my $webpage = get(“http://www.w3mentor.com”);
my $htmltree = HTML::TreeBuilder->new->parse($webpage);
my $output = HTML::FormatText->new();

print $output->format($htmltree);


Post a form programatically using Perl LWP

#!/usr/bin/perl -w
use LWP;
use strict;
my $browser = LWP::UserAgent->new(agent => 'Firefox');
 
$result = $browser->post('http://www.w3mentor.com/form.cgi',
[
'name' => 'mani',
'email' => 'mani@w3mentor.com',
'zip' => '54481'
]);
 
print $result->content;

Retrieve a web page by setting user agent manually using Perl LWP

#!/usr/bin/perl -w
use LWP;
use strict;
 
my $browser = LWP::UserAgent->new(agent => 'Mozilla Firefox 3.2');
my $result = $browser->get("http://www.w3mentor.com");
die "An error occurred: ", $result->status_line( ) unless
$result->is_success;
 
#Do something more meaningful with the content than this!
print $result->content;

Retrieving a Web Page with get() in Perl LWP

The get() function from LWP::Simple enables you to quickly and easily retrieve a web page using the GET method.

#!/usr/bin/perl -w
use LWP::Simple;
use strict;
 
my $webpage = get("http://www.w3mentor.com/");
 
if (($webpage) && (grep {/tutorials/} $webpage)) {
print "I found the text\n";
}

Using the mirror() Function with Perl LWP

The mirror() function works in much the same was as the getstore() function, but also includes a check to compare the modification time of the local file and the modification time of the remote resource, based on the If-Modified-Since response header.

#!/usr/bin/perl -w
use 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);

Using is_success() with getstore() in Perl LWP

The getstore() function takes the output of a web page and automatically stores it in an external file. The getstore() function also returns the status of the GET method and sets is_success() if the status is in the 200 range. It sets is_error() if the status is in the 400 or 500 range.

#!/usr/bin/perl -w
use LWP::Simple;
use strict;
 
my $status = getstore("http://www.braingia.org/","/tmp/w3file.txt");
unless (is_success($status)) {
die "Couldn't retrieve page: $status";
}
 
open (PAGE, "/tmp/w3file.txt") or die "$!";
while (<PAGE>) {
print();
}
 
close(PAGE);

Retrieve a webpage using LWP in Perl

#!/usr/bin/perl -w
use LWP;
use strict;
my $browser = LWP::UserAgent->new(agent => 'Mozilla Firefox 3.2');
my $result = $browser->get("http://www.w3mentor.com");
die "An error occurred: ", $result->status_line( ) unless \
$result->is_success;
#Do something more meaningful with the content than this!
print $result->content;

Iterate over environment variables in Perl

foreach $key (keys %ENV) {
print "Environment key $key is $ENV{$key}\n";
}

Check for Acceptable File Types during upload in Perl

When a file with a Content-Type that isn’t text/html is uploaded to this CGI script, its output indicates that only HTML types are allowed.

#!/usr/bin/perl
 
use strict;
use CGI qw/:standard/;
my $q = new CGI;
 
my $filename = $q->param('uploaded_file');
my $contenttype = $q->uploadInfo($filename)->{'Content-Type'};
 
print header;
print start_html;
if ($contenttype !~ /^text\/html$/) {
print "Only HTML is allowed<P>";
print end_html;
exit;
} else {
print "Type is $contenttype<P>";
}
 
print end_html;

Display the Content-Type of an Uploaded File in Perl

#!/usr/bin/perl
use strict;
use CGI qw/:standard/;
my $q = new CGI;
my $filename = $q->param('uploaded_file');
my $contenttype = $q->uploadInfo($filename)->{'Content-Type'};
print header;
print start_html;
print "Type is $contenttype<P>";
print end_html;