Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

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

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



  • 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 meditating upon the Monastery: (4)
As of 2024-04-19 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found