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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.