How close? Very close. Why don't you install Moose and then try this code out. Feel free to ask lots of questions.

#!/usr/bin/env perl package Base; use Moose; has title => ( is => 'rw', isa => 'Any' ); has start_year => ( is => 'rw', isa => 'Any' ); sub to_string { my $self = shift; return join "\n", ' Title: ' . $self->title, 'Start Year: ' . $self->start_year, ; } package Movie; use Moose; extends 'Base'; has based_on => ( is => 'rw', isa => 'Any' ); has company => ( is => 'rw', isa => 'Any' ); sub to_string { my $self = shift; return join "\n", $self->SUPER::to_string, ' Based On: ' . $self->based_on, ' Company: ' . $self->company, '', ; } package TV; use Moose; extends 'Base'; has end_year => ( is => 'rw', isa => 'Any' ); sub run_time { my $self = shift; return $self->end_year - $self->start_year; } sub to_string { my $self = shift; return join "\n", $self->SUPER::to_string, ' End Year: ' . $self->end_year, ' Run Time: ' . $self->run_time, '', ; } package main; use strict; use warnings; my %movies_data = ( Firefly => { title => 'Firefly', start_year => '2002', end_year => '2003', media => 'tv', }, 'The Avengers' => { title => 'The Avengers', start_year => '1998', end_year => '', media => 'film', based_on => 'television series', company => 'Thames Television', }, ); my @objects; for (keys %movies_data) { if ($movies_data{$_}{media} eq 'film') { push @objects, Movie->new( $movies_data{$_} ); } elsif ($movies_data{$_}{media} eq 'tv') { push @objects, TV->new( $movies_data{$_} ); } } print $_->to_string, $/ for @objects;

UPDATE (5/6/2015)
I was not honestly expecting the OP to be sincere, but i really love these kinds of "problems" and am glad to have posted regardless. Something else i would love to share is MooseX::AbstractFactory. By only adding one new class (and not changing a line in the others) the decision of which object to create is moved to the data itself. Very nice:

package Factory; use MooseX::AbstractFactory; implementation_class_via sub { shift }; package main; use strict; use warnings; my %movies_data = ( Firefly => { title => 'Firefly', start_year => '2002', end_year => '2003', media => 'TV', # <--- real class name }, 'The Avengers' => { title => 'The Avengers', start_year => '1998', end_year => '', media => 'Movie', # <--- real class name based_on => 'television series', company => 'Thames Television', }, ); my @objects; for (keys %movies_data) { push @objects, Factory->create( $movies_data{$_}{media}, $movies_d +ata{$_} ); } print $_->to_string, $/ for @objects;

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to Re: How do I go from procedural to object oriented programming? by jeffa
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.