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;