Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Question regarding proper handling of a Hash/HashRef structure in Moose-variantsby atcroft (Abbot) |
on Dec 22, 2021 at 05:44 UTC ( [id://11139816]=perlquestion: print w/replies, xml ) | Need Help?? |
atcroft has asked for the wisdom of the Perl Monks concerning the following question: To the members of the body monktorate, greetings. I seek your wisdom. I am trying to teach myself to use Moose (or the associated variations, such as Mouse, Moo, Mo, etc.). For my learning project, I wanted to do something I felt more useful than the Animal example, so for my purposes I wanted to represent a particle. I created a StationaryParticle, then extended it to a MoveableParticle. Because I want it to move within a limited rectangular space, I began trying to add a Boundary object to the system, which I added as a singleton for now (because in most cases particles would have the same boundaries). This is when I came to my problem. In my way of thinking I would either group a boundary based on aggregation (min/max) then dimension (x/y), or vice versa, so I might define either of the following: (For the remainder of the code, I will go with the aggregation-first model, although the issue occurs either way.) When I try to code that into Moo (or Moose), I'm not sure how best to handle that object. I would think I should be able to add the limit through a method, but I cannot seem to reason out what that method should look like. If I break encapsulation I could it could be as easy as $wall->limit->{min}{x} = 0, but I don't want to be *that guy* who tromps through code (and for whom it blows up if the Boundary objects change later.)
I then tried a 'before' call to try to allow me to hand it a HoH and set the values there, or pass in a HoH with an undefined value to hopefully return the value:
What I see with this is that I appear to be replacing the singleton when I try to use the undef setting to retrieve the value. What is the proper way to do what I am trying to accomplish? Thank you for your time and attention, and any guidance/assistance you may provide. Have a great day, and stay safe! sscce.pl:
Back to
Seekers of Perl Wisdom
|
|