walto has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl # # package MeteoalarmCountries; use strict; use warnings; use Carp; use LWP; sub new { my $class = shift; my $self = {}; $self->{user_agent} = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6; + .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 3.1; .NET CLR +2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)'; bless( $self, $class ); return $self; } sub countries { my %warnpics = ( 'wf_21.jpg' => 'Wind 1', 'wf_22.jpg' => 'Snow/Ice 1', 'wf_23.jpg' => 'Thunderstorm 1', 'wf_24.jpg' => 'Fog 1', 'wf_25.jpg' => 'Extreme high temperature 1', 'wf_26.jpg' => 'Extreme low temperature 1', 'wf_27.jpg' => 'Coastal Event 1', 'wf_28.jpg' => 'Forestfire 1', 'wf_29.jpg' => 'Avalanches 1', 'wf_210.jpg' => 'Rain 1', ); my $self = shift; my %passed_params = @_; for my $parm ( keys %passed_params ) { $self->{$parm} = $passed_params{$parm}; }
my $url = 'http://www.meteoalarm.eu/index.php?lang=&AT=' . $self->{type} . '&day=' . $self->{day}; my $ua = LWP::UserAgent->new; $ua->agent( $self->{user_agent} ); my $res = $ua->request( HTTP::Request->new( GET => $url ) ); croak " $res->status_line, \n"unless ($res -> is_success); my $page = $res->content; my @splitted_pages = split '<a href=', $page; foreach my $splitted (@splitted_pages) { if ( $splitted =~ /id="(\w\w)" alt="(.+?)" ><span class="warn"><img src="Bilder\/wf\/wf_ +\d{2,3}.jpg">/ ) { my %warn; my $country_code = $1; my $country = $2; $self->{$country_code}->{'fullname'}= $country; my @warnings = $splitted =~ /<img src="Bilder\/wf\/(wf_\d{2,3}.jpg)">/g +; my @literal_warnings = map { $warnpics{$_} } @warnings; foreach my $warn (@literal_warnings) { my @part = split ' ', $warn; $warn{ lc $part[0] } = $part[1]; } $self->{$country_code}->{'warnings'} = \%warn; } } delete $self->{'user_agent'}; delete $self->{'day'}; delete $self-> {'type'}; return $self; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How can I keep object arguments private
by toolic (Bishop) on May 23, 2010 at 16:53 UTC | |
by walto (Pilgrim) on May 23, 2010 at 17:45 UTC | |
by pemungkah (Priest) on May 24, 2010 at 05:47 UTC | |
|
Re: How can I keep object arguments private
by FalseVinylShrub (Chaplain) on May 24, 2010 at 06:18 UTC | |
by walto (Pilgrim) on May 24, 2010 at 08:37 UTC | |
|
Re: How can I keep object arguments private
by ikegami (Patriarch) on May 24, 2010 at 06:21 UTC |