Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The biggest difference I can think of between procedural programming and OO programming is where state logic is maintained.

In procedural programming, state logic tends to be maintained by the programmer within the main body of the program. This means that in the main body of the program you have to maintain all the gory guts of everything you are working with. In order to try and deal with all of this, procedural programmers do tend towards an OO style, in an attempt to maintain sanity.

In OO programming state logic is maintained by the objects themselves (I am a movie, I know what state I am in and I know how to modify that state based on the actions I let you perform on me). This leaves the main body of the program free to describe the logic of what is happening.

use 5.16.2; use warnings FATAL => qw( all ); package movie; sub new { my $class = shift; my %params = @_; my $self = bless {}, $class; $self->title($params{title}); $self->startyear($params{startyear}); $self->endyear($params{endyear}); $self->media($params{media}); $self->basedon($params{basedon}); $self->company($params{company}); return $self; } sub title { my $self = shift; my $title = shift; $self->{title} = $title if $title; return $self->{title}; } sub startyear { my $self = shift; my $startyear = shift; $self->{startyear} = $startyear if $startyear; return $self->{startyear}; } sub endyear { my $self = shift; my $endyear = shift; $self->{endyear} = $endyear if $endyear; return $self->{endyear}; } sub media { my $self = shift; my $media = shift; $self->{media} = $media if $media; return $self->{media}; } sub basedon { my $self = shift; my $basedon = shift; $self->{basedon} = $basedon if $basedon; return $self->{basedon}; } sub company { my $self = shift; my $company = shift; $self->{company} = $company if $company; return $self->{company}; } sub print { my $self = shift; say "I am a movie and my title is $self->{title}"; } package library; sub new { my $class = shift; my %params = @_; my $self = bless {}, $class; $self->name($params{name}); $self->add_movie($_) for @{$params{movies}}; return $self; } sub name { my $self = shift; my $name = shift; $self->{name} = $name if $name; return $self->{name}; } sub add_movie { my $self = shift; my $movie = shift; $self->{movies}{$movie->title()}{movie} = $movie; $self->{movies}{$movie->title()}{location} = 'in'; } sub print { my $self = shift; say "I am a library called '".$self->name()."' and contain the fol +lowing movies:"; say "\t".$_->{movie}->title() foreach values %{$self->{movies}}; } package main; my %movies_data = ( 'Firefly' => { 'title' => 'Firefly', 'startyear' => '2002', 'endyear' => '2003', 'media' => 'tv', }, 'Criminal Minds' => { 'title' => 'Criminal Minds', 'startyear' => '2005', 'endyear' => 'tbd', 'media' => 'tv', }, 'The 10th Kingdom' => { 'title' => 'The 10th Kingdom', 'startyear' => '2000', 'endyear' => '', 'media' => 'miniseries', }, 'Iron Man' => { 'title' => 'Iron Man', 'startyear' => '2008', 'endyear' => '', 'media' => 'film', 'basedon' => 'comics', 'company' => 'Marvel Comics', }, 'Tin Man' => { 'start year' => '2007', 'title' => 'Tin Man', 'media' => 'miniseries', 'basedon' => 'novel', 'company' => 'L. Frank Baum' }, 'The Avengers (1998)' => { 'title' => 'The Avengers (1998)', 'startyear' => '1998', 'media' => 'film', 'basedon' => 'television series', 'company' => 'Thames Television', }, ); my $library = library->new(name => "Lucky's"); foreach my $movie (values %movies_data) { $library->add_movie(movie->new(%$movie)); } $library->print();

See how in main, there is now no logic about how to update state? (I'm using your hash to pass the required values to the movie object - The object itself has the logic for using (or not) the values provided). Main is now all about the logic of what you want to do (make a movie, make a library, add a movie to a library, print out the contents of the library, etc)


In reply to Re: How do I go from procedural to object oriented programming? by SimonPratt
in thread How do I go from procedural to object oriented programming? by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-03-29 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found