my %movies_data = ( 'Firefly' => { 'title' => 'Firefly', 'start year' => '2002', 'end year' => '2003', 'media' => 'tv', 'based on' => undef, 'company' => undef, }, 'Criminal Minds' => { 'title' => 'Criminal Minds', 'start year' => '2005', 'end year' => 'tbd', 'media' => 'tv', 'based on' => undef, 'company' => undef, }, 'The 10th Kingdom' => { 'title' => 'The 10th Kingdom', 'start year' => '2000', 'end year' => '', 'media' => 'miniseries', 'based on' => undef, 'company' => undef, }, 'Iron Man' => { 'title' => 'Iron Man', 'start year' => '2008', 'end year' => '', 'media' => 'film', 'based on' => 'comics', 'company' => 'Marvel Comics', }, 'Tin Man' => { 'start year' => '2007', 'title' => 'Tin Man', 'end year' => '', 'media' => 'miniseries', 'based on' => 'novel', 'company' => 'L. Frank Baum' }, 'The Avengers (1998)' => { 'title' => 'The Avengers (1998)', 'start year' => '1998', 'end year' => '', 'media' => 'film', 'based on' => 'television series', 'company' => 'Thames Television', }, ); #### package Movies::LookUp; use strict; use warnings FATAL => qw( all ); use Lingua::EN::Inflect qw(A PL_N NUM NUMWORDS inflect); ############################# # insert %movies_data here! # ############################# my $current_year = (localtime())[5] + 1900; # returns the entire movies hash. sub movie_hash { return %movies_data; } # returns a hash ref for a single movie. sub movie { my ($movie,$caller) = @_; if (!$movies_data{$movie}) { warn $caller ? "$caller: $movie not in database" : "$movie not in database"; } return $movies_data{$movie}; } # returns the start year of a movie sub start_year { my ($imovie) = @_; my $movie = movie($imovie,'start_year'); return $movie->{'start year'}; } # returns a numeric end year of a movie for comparisons sub end_year { my ($imovie) = @_; my $movie = movie($imovie,'end_year'); my $end_year = $movie->{'end year'} ? ($movie->{'end year'} eq 'tbd' ? $current_year : $movie->{'end year'}) : $movie->{'start year'}; return $end_year; } # returns a string with the run time of a television series. sub run_time { my ($imovie) = @_; my $movie = movie($imovie,'run_time'); my $run_text = undef; if ($movie->{'media'} eq 'tv') { if ($movie->{'end year'} eq 'tbd') { $run_text = "which is still running"; } elsif (end_year($movie->{'title'}) - start_year($movie->{'title'}) > 0) { my $run_time = end_year($movie->{'title'}) - start_year($movie->{'title'}); $run_text = inflect("which ran for NUMWORDS($run_time) PL_N(year,$run_time)"); } } return $run_text; } # returns the media type of a movie sub media { my ($imovie) = @_; my $movie = movie($imovie,'media'); my $media = $movie->{'media'} eq 'tv' ? 'television series' : $movie->{'media'}; return $media; } # returns what the movie is based on and by who or what sub basis { my ($imovie) = @_; my $movie = movie($imovie,'basis'); my $basis = $movie->{'based on'} ? qq(based on the $movie->{'based on'} by $movie->{'company'}) : undef; return $basis; } # returns a string with nearly all the properties of a movie sub movie_is { my ($movie) = @_; my $start = start_year($movie); my $media = media($movie); my $basis = basis($movie); my $runtime = run_time($movie); my $movie_is = A(join_defined(' ',[$start,$media,$basis,$runtime])).'.'; return $movie_is; } 1; #### use strict; use warnings FATAL => qw( all ); use Movies::LookUp qw(movies movie_is); my %movies = movie_hash(); for my $movie (sort keys %movies) { my $movie_is = movie_is($movie); print "

$movie $movie_is

\n"; }