Fellow Monks,
I try to get a grip on objects in perl and there is a vast amount of questions coming up.
I wrote a module for retrieving data from a website. I call the object with a (only one at the moment) method and expect a hashref of the return value. But also the method options and object properties from the constructor are included in the return value. In order just to get the wanted values in the hashref I delete the values from the object before they are returned. But there will be problems when I call the method repeatedly.
Here is the code:
#!/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; }
Apparently I also need a hint with the readmore tag.

In reply to How can I keep object arguments private by walto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.