Checking the caller smells of bad design. Moreover, what should happen if the Child is created in, let's say, main, or worse, a descendant of Parent?

I'd create a role for creating children, and let each class consume the role while parameterizing its child's context:

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; { package Child::Creator; use MooseX::Role::Parameterized; parameter context => (isa => 'Str', required => 1); role { my ($p) = @_; my $context = $p->context; has _child_context => (is => 'ro', default => $context); sub create_child { 'Child'->new(context => shift->_child_context); } }; } { package Parent; use Moose; with 'Child::Creator' => {context => 'parent'}; __PACKAGE__->meta->make_immutable; } { package Teacher; use Moose; with 'Child::Creator' => {context => 'teacher'}; __PACKAGE__->meta->make_immutable; } { package Child; use Moose; use Moose::Util::TypeConstraints qw{ enum }; has context => (is => 'ro', required => 1, isa => enum([qw[ parent teacher ]])); __PACKAGE__->meta->make_immutable; } my $p = 'Parent'->new; my $ch_p = $p->create_child; say $ch_p->context; my $t = 'Teacher'->new; my $ch_t = $t->create_child; say $ch_t->context; # Attribute (context) is required at constructor Child::new (defined a +t ./1.pl line 44) line 30 my $ch = 'Child'->new; # Attribute (context) does not pass the type constraint because: Valid +ation failed for '__ANON__' with value "unknown" at constructor Child +::new (defined at ./1.pl line 44) line 39 my $ch = 'Child'->new(context => 'unknown');

But you haven't described what you need the context for, there might be better ways to get there.

Update:

The parameterized role is not needed here, it just keeps the context closer to the role consumption. You can use a plain Moose role, too (but there are cases where you can't replace a parameterized role with a plain one):

{ package Child::Creator; use Moose::Role; requires 'child_context'; sub create_child { 'Child'->new(context => shift->child_context); } } { package Parent; use Moose; with 'Child::Creator'; sub child_context { 'parent' }; __PACKAGE__->meta->make_immutable; } ...

($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,

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.