http://qs321.pair.com?node_id=1124071

Lady_Aleena has asked for the wisdom of the Perl Monks concerning the following question:

(Jenda corrected me on what my subroutines are. They are procedures not functions. The title of this node and where ever the word, or any derivatives of, function has been changed to procedure.)

Going from procedural programming to object oriented programming has been on my mind a lot lately. I have been told by a couple of people my code is very close to being OO, however when I gave OO a try the first time, I was told I was doing it all wrong. I would like to see how close I am, but I am having a hard time learning objects because the tutorials I have found start writing code right away. I have yet to find a tutorial which starts with the objective of the objects being written. For example I want...

Criminal Minds is a 2005 television series which is still running.

Iron Man is a 2008 film based on comics by Marvel Comics.

The tutorials also do not show the data being munged up front like...

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', }, );

They all start right in on the objects, leaving me completely in the dark about what the end goal for the objects is. From above you know my objective and have the data to reference while reading the procedures I wrote to get to my objective. The whole module is here if you would like to see the bigger picture.

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 o +n'} 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;

Now putting it all together to get my objective.

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 "<p>$movie $movie_is</p>\n"; }

I would like to know how close I am to having objects, what they would look like if I am close, and if there is anything which does not fit into OO. Is there anywhere I need to change my thinking (which will be hard since I have been doing things as above for a long while now)?

If any of the OO tutorials were written with the objective, data, code, and wrap-up in that order; I might get them.

Another reason I am doing this is to get my mind off of my pain and impending surgery. I am doing everything I can think of to get my mind off of them and stave off panic. Would you please help me?

I hope I am not asking too much. Please sit back and enjoy a cookie.

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena