#!perl use strict; use warnings; use LWP::UserAgent; =over 4 =item B my $text = fetch($url, $regex [, $http_proxy]); Busca uma página de HTML endereçada por C<$url> e, se o conteúdo combina com a expressão regular C<$regex>, retorna o texto encontrado. my $url = 'http://foo'; my $text = fetch($url, qr/(.*?)/); print "Texto: $text\n" if $text; =back =cut sub fetch { my $url = shift; my $regex = shift; my $http_proxy = shift; my $ua = LWP::UserAgent->new; $ua->proxy(['http'], $http_proxy) if $http_proxy; my $r = $ua->get($url); if ($r->is_success) { return ($r->content =~ /$regex/) ? $1 : undef; } else { die $r->status_line; } } my $url = 'http://mail.pm.org/pipermail/triangulo-pm/2006-December/thread.html'; my $re = qr|
  • \[Triangulo-pm\] (.*?)$|m; # Esta expressão regular foi projetada para combinar e extrair # "Perl em Windows" em textos como: #
  • [Triangulo-pm] Perl em Windows my $proxy = 'http://user:pass@proxy-name-or-ip:3128/'; # como estou atrás de um proxy com autenticação, preciso # de definir este terceiro argumento (que é opcional) my $text = fetch($url, $re, $proxy); print "Texto: $text\n" if $text;