in reply to Re: What is a reliable way to get the package of the code creating a Moose object?
in thread What is a reliable way to get the package of the code creating a Moose object?

Ah, very cool. Thanks.

What I'm trying to achieve is to have the Child object behave differently depending on what created it. So if the Child object has a method called reply, it will respond with Yes, Mommy. if the Parent package created it and Yes, Teacher if the Teacher package created it. I was going to use the context property to determine how it the object should respond with something like this:

sub reply { my $self = shift; if ($self->context eq 'teacher') { return "Yes, Teacher"; else { return "Yes, Mommy."; } }

But as I think about it, this could get messy. I think what I really need to do is create a parent class for Child with default methods and then override those methods for the different type of Child subclasses. It would be nice to have the calling package be able to construct the correct Child subclass automatically I think I can use your suggestions here to help me achieve that.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re^3: What is a reliable way to get the package of the code creating a Moose object?
by Corion (Patriarch) on Jul 12, 2018 at 13:39 UTC

    Would it maybe help to have the Teacher create Pupil objects, and the Parent create Child objects? The Pupil could be an object that has a Child instance and reflects most methods, or it could inherit from the Child class?

      Yeah, it probably makes the most sense to do it like this. That would be a much more standard design pattern even if it's a little more boring.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

Re^3: What is a reliable way to get the package of the code creating a Moose object?
by choroba (Cardinal) on Jul 12, 2018 at 13:35 UTC
    > I think what I really need to do is create a parent class

    The role is like an abstract parent class in this case. And you can "override" the method when consuming the role.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,