Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: How to sub class-es

by GrandFather (Sage)
on May 15, 2020 at 07:52 UTC ( #11116806=note: print w/replies, xml ) Need Help??


in reply to How to sub class-es

Since we are all making wild guesses as to what you actually want to achieve I thought I'd add my own version.

If you are simulating or writing a control system for a robot (it comes to much the same thing) then Robo should be a container for a collection of RoboParts derived objects. Robo probably isn't a RoboPart, but that depends a bit on what your end game is. If the Robo instance takes external instructions from a console then it may well benefit from deriving from RoboPart. If instead it is a self contained instance that manages the various RoboPart objects then it doesn't derive derive from RoboPart.

Maybe you'd like to tell us what you want to achieve and not how you think yo might best achieve it? Then we can help you get the class structure right to support your objective.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^2: How to sub class-es
by exilepanda (Pilgrim) on May 15, 2020 at 11:31 UTC
    If talking out of the code, I'd take Robo as a control point. For example, if left foot steps forward, the right arm moves forward too, to balance the body weight. But under the arm itself, if certain angle is reached, it stops, and I will leave it to Robo::Arm handle by itself. Back to the code, I've updated to my post. Any advise for me? Thx!

      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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2020-09-27 17:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (142 votes). Check out past polls.

    Notices?