berm has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl print "Content-type: text/html\n\n"; #vars $LocationID = $ENV{QUERY_STRING}; $CelciusFahrenheit = "c"; if ($LocationID =~ ",") { ($LocationID,$CelciusFahrenheit)=split(/\,/,$LocationID); $CelciusFahrenheit = "f"; } #if $url = "http://weather.yahooapis.com/forecastrss?p=$LocationID&u=$Celc +iusFahrenheit"; #LWP::UserAgent use LWP::UserAgent; $ua = new LWP::UserAgent; $ua->timeout(180); $request = new HTTP::Request('get',"$url"); $request->header('Accept' => 'text/html'); $response = $ua->request($request); $Content = $response->{_content}; #LWP::Simple if response code != 200 and response message ne OK if ($response->{_rc} != 200 && $response->{_msg} ne "OK") { use LWP::Simple; $Content = get("$url"); } #if #parse package MyParser; use base qw(HTML::Parser); #set state flags my $title_flag = 0; my $title_count = 0; #vars set to "" my $Title = ""; #start---------------------------------------------------------------- +---------- sub start { my ($self, $tagname, $attr, $attrseq, $origtext) = @_; #Title if ($tagname =~ /^title$/i) {$title_flag = 1;} #if } #sub #text----------------------------------------------------------------- +---------- sub text { my ($self, $text) = @_; #Title if ($title_flag == 1 && $title_count == 0) {$Title = "$text"; $titl +e_count++;} #if } #sub #end------------------------------------------------------------------ +---------- sub end { my ($self, $tagname, $origtext) = @_; if ($tagname =~ /^title$/i) {$title_flag = 0;} } #sub package main; my $html = <<EOHTML; $Content EOHTML my $parser = MyParser->new; $parser->parse( $html ); $parser->eof; #end parse------------------------------------------------------------ +---------- #--------------------------------------------------------------------- +---------- #print print <<EOF; <html> <head> <title>$Title</title> </head> <body> $Title </body> </html> EOF exit;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: MyParser Question
by ikegami (Patriarch) on Jul 26, 2007 at 02:48 UTC | |
| |
|