package WWW::Baseball::NPB; use strict; use vars qw($VERSION); $VERSION = 0.03; use LWP::Simple; use WWW::Baseball::NPB::Game; use vars qw($YahooURL); $YahooURL = 'http://sports.yahoo.co.jp/baseball/'; sub croak { require Carp; Carp::croak(@_) } sub new { my $class = shift; my $self = bless { games => [] }, $class; $self->_parse_body($YahooURL); return $self; } sub games { my($self, $league) = @_; unless ($league) { return @{$self->{games}}; } return grep { $_->league eq $league } @{$self->{games}}; } sub _parse_body { my($self, $url) = @_; my $body = LWP::Simple::get($url) or croak("Can't get content of $url"); my $re = $self->_match_pattern; while ($body =~ m/$re/g) { $self->_add_game($1, $2, $3, $4, $5, $6, $7); } } my %league = (cl => 'central', pl => 'pacific'); sub _add_game { my($self, $home, $home_score, $visitor_score, $league, $status, $visitor, $stadium) = @_; for ($home_score, $visitor_score) { s,(.*),$1,; } push @{$self->{games}}, WWW::Baseball::NPB::Game->new( home => $home, visitor => $visitor, score => { $home => $home_score, $visitor => $visitor_score, }, league => $league{$league}, status => $status, stadium => $stadium, ); } sub _match_pattern { return <<'RE';