Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^2: [Moose] Critique request on my Moose design (anti?)pattern

by three18ti (Scribe)
on Feb 01, 2014 at 08:27 UTC ( #1072949=note: print w/replies, xml ) Need Help??

in reply to Re: [Moose] Critique request on my Moose design (anti?)pattern
in thread [Moose] Critique request on my Moose design (anti?)pattern

Hey thanks for the reply.

For instance, if I'm building a Car object, I'll probably want to declare an "Engine" object and then handle the delegation of that objects methods in the Car object.


package Car::Engine; use 5.010; use Moose; use strict; use warnings; has [qw(cylinder rpm fuel)] { is => 'rw', isa => 'Int', } sub get_rpm { my $self = shift; my $curr_rpm = $self->rpm / $self->cylinder; } package Car; use 5.010; use Moose; use strict; use warnings; has [qw(engine_cylinder engine_rpm engine_fuel)] => ( is => 'rw', isa => 'Int', ); has engine => ( is => 'rw', isa => 'Car::Engine', default => { Car::Engine->new( cylinder => $_[0]->engine_cylinder, rpm => $_[0]->engine_rpm, fuel => $_[0]->engine_rpm, ); }, handles => { get_rpm => 'get_rpm', } ); package main; use 5.010; use strict; use warnings; my $car = Car->new( engine_cylinder => '8', engine_rpm => '8000', engine_fuel => '1000', ); say $car->get_rpm;

(actually, the equation for rpms doesn't make any sense, but it was just to show that ->get_rpm is different than ->rpm)

In the real world I'm working on a library to manage VMs and I've created several objects to represent things like the Hypervisor Cluster and the VM (which represents all the attributes for the vm, the actual running vm is then represented by a different object which I'm trying to figure out how to handle).

I don't know what the correct term for "Car::Engine" would be in this case which is why I called it a "sub-object" in my previous comment.

I just seem to be using the pattern a lot which results is a lot of duplicate typing (or cut and paste) which got me to thinking maybe there's a better way to go about using objects in my primary (in this case "Car") object.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1072949]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2018-06-25 02:53 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.