Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: How do I go from procedural to object oriented programming?

by jdporter (Paladin)
on Apr 23, 2015 at 15:22 UTC ( [id://1124414]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How do I go from procedural to object oriented programming?
in thread How do I go from procedural to object oriented programming?

And here's my OO version of your (abbreviated) module:

package Movie; use strict; use warnings FATAL => qw( all ); use Lingua::EN::Inflect qw(A PL_N NUM NUMWORDS inflect); my $current_year = (localtime())[5] + 1900; sub title { $_[0]->{'title'} } # returns the start year of a movie sub start_year { $_[0]->{'start year'} } # returns a numeric end year of a movie for comparisons sub end_year { my $self = shift; $self->{'end year'} or return $self->{'start year'}; $self->{'end year'} eq 'tbd' and return $current_year; $self->{'end year'} } # returns a string with the run time of a television series. sub run_time { my $self = shift; $self->{'media'} eq 'tv' or return(); $self->{'end year'} eq 'tbd' and return "which is still running"; my $sy = $self->start_year; my $ey = $self->end_year; $ey == $sy and return "which ran for less than a year"; my $run_time = $ey - $sy; inflect("which ran for NUMWORDS($run_time) PL_N(year,$run_time)"); } # returns the media type of a movie sub media { my $self = shift; $self->{'media'} eq 'tv' ? 'television series' : $self->{'media'} } # returns what the movie is based on and by who or what sub basis { my $self = shift; $self->{'based on'} or return(); qq(based on the $self->{'based on'} by $self->{'company'}) } # returns a string with nearly all the properties of a movie sub movie_is { my $self = shift; A(join(' ', grep { defined $_ } $self->start_year, $self->media, $ +self->basis, $self->run_time )).'.'; } 1;

Notice that I deleted the stuff about "insert %movies_data here!". Because this isn't where it should go. Instead, it should be placed in a "higher" level of the code -- whether that's the main program, or a containing module, or what. This module is only for representing individual movie records. Hence the name "Movie", not "Movies".

Also, I renamed that last method (was "movie_is"), and I added a title method.

Also, I replaced your fatal warn() with a croak, so that I could get a full stack dump. You can revert that easily enough.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1124414]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found