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)
-
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.