Perl - How to Parse a web page

Posted on giovedì 23 dicembre 2010 by Ivano Binetti

The following simple perl script allows to automatically search some text, like html link, into a web page:

#!/usr/bin/perl -w
use strict;

#this module allows to delete duplicate entries
use List::MoreUtils qw(uniq);

# this is the module tointeract with web page
use LWP::Simple;

#variables declaration
my @match;
my @match_uniq;
my $file = <your temp file>";

#I'm used the "get" method of LWP::Simple module 
my $webpage = get("http://<your url>);

#write my web page into the file
open WH, "> $file" or die $!;
print WH $webpage;
close WH;

#put the file into a array to manipulate it
open RH, "< $file" or die $!;
my @file = <RH>;
close RH;

#search server name and push them into array @match
foreach (@file) {
if (/https?:\/\/(\w+\.\w+\.\w+)/) {
push (@match, $1);
}
}

#remove duplicate entries
@match_uniq= uniq(@match);

#strip out "www."
foreach (@match_uniq) {
s/www\.//g;
}
foreach (@match_uniq) {
print "$_\n";
}

Perl - Shell Subroutine

Posted on martedì 21 dicembre 2010 by Ivano Binetti

How to use Shell Subroutine to simply install CPAN module

The simplest and quickest method to install a CPAN module is to use the perl shell subroutine.
To run the shell subroutine from command-line, use:

# perl -MCPAN -e shell

This command runs Perl, loads the "CPAN" module into memory and runs the shell soubroutine.

So to install, for example, the WWW::Mechanize module you have to simply execute:

cpan> install WWW::Mechanize

and  the shell subroutine will connect to the internet cpan website and will download and automatically install your module.