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

Is there a reason why the caller can't set the context? e.g.
use strict; use warnings; package Child; use Moose; has 'context' => (is => 'rw'); package Teacher; my $tom = Child->new(context => 'Teacher'); print $tom->context . "\n"; package Parent; my $kit = Child->new(context => 'Parent'); print $kit->context . "\n";

One world, one people

  • Comment on Re: What is a reliable way to get the package of the code creating a Moose object?
  • Download Code

Replies are listed 'Best First'.
Re^2: What is a reliable way to get the package of the code creating a Moose object?
by tobyink (Canon) on Jul 15, 2018 at 08:23 UTC

    Better:

    package Child; # insert usual Moose stuff has context => (is => 'ro', isa => Str, init_arg => '_context', requir +ed => 1); sub new_for_parent { my $class = shift; $class->new(@_, _context => 'parent'); } sub new_for_teacher { my $class = shift; $class->new(@_, _context => 'teacher'); }

    The parent and teacher create the child via different constructors.

    (People don't think about creating custom constructors as often as they should.)