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

comment on

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

So my wild guess was actually pretty close to the mark. You don't need Moose for a has-a relationship, a simple array or hash is essentially that. You are back at the container of RoboPart derived objects I mentioned above. Your Robo object then is just a container that knows how to send messages between the different parts and tosses out the odd message of its own. Consider:

use strict; use warnings; package RoboPart; sub new { my ($class, %options) = @_; return bless \%options, $class; } sub dispatch { my ($self, $verb, @params) = @_; my $handler = $self->can($verb); die ref $self . " can't $verb\n" if !defined $handler; return $handler->($self, @params); } sub does { my ($self, $verb) = @_; return $self->can($verb); } package RoboArm; use parent -norequire, 'RoboPart'; sub pick { my ($self, $target) = @_; print "I am grabbing $target\n"; } package RoboFeet; use parent -norequire, 'RoboPart'; sub walk { my ($self) = shift; my ($x, $y) = @{$self->{Position}} ; if ( $x > 10 || $y > 10 ) { print "Out of boundary. I stop walk\n"; } else { print "I start walk from pos $x $y\n"; } } package Robo; sub new { my ($class) = @_; return bless { Arm => RoboArm->new(), Feet => RoboFeet->new(Position => [11, 0]), }, $class; } sub send { my ($self, $verb, @params) = @_; for my $part (values %$self) { $part->dispatch($verb, @params) if $part->does($verb); } } package main; my $robo = Robo->new(); $robo->send("pick", "Apple"); $robo->send("walk");

Prints:

I am grabbing Apple Out of boundary. I stop walk
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

In reply to Re^3: How to sub class-es by GrandFather
in thread How to sub class-es by exilepanda

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 making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-18 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found