Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by anonymized user 468275 (Curate)
on Jul 13, 2018 at 10:38 UTC ( [id://1218433]=note: print w/replies, xml ) Need Help??


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.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-25 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found