Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by jeffa (Bishop)
on Apr 20, 2015 at 21:42 UTC ( [id://1124074]=note: print w/replies, xml ) Need Help??


in reply to How do I go from procedural to object oriented programming?

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)

Replies are listed 'Best First'.
Re^2: How do I go from PROCEDURAL to object oriented programming?
by Anonymous Monk on Apr 20, 2015 at 23:17 UTC
    Moose under CGI? Just say no
      Moose under C G I? Just say no

      What a completely pointless comment. Are you suggesting that one cannot use Moose because they are still using C G I? The reason I put spaces in "C G I" is because if one searches for C G I without spaces, the only match is yours. This means you are the only one who mentioned it at the time. Why? So which is it: use C G I or use Moose?

        Moose under C G I? Just say no What a completely pointless comment. Are you suggesting that one cannot use Moose because they are still using C G I? The reason I put spaces in "C G I" is because if one searches for C G I without spaces, the only match is yours. This means you are the only one who mentioned it at the time. Why? So which is it: use C G I or use Moose?

        Lady_Aleena is writing code to run under CGI, Moose is too slow when run under CGI -- too much overhead for no benefit

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 17:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found