http://www.perlmonks.org?node_id=1020336

blue_cowdawg has asked for the wisdom of the Perl Monks concerning the following question:

OK: my sick mind has come up with something and I'm trying to figure out how to do it:

Let's say I have three modules defined as:

package Top; use Moose; 1; package SubordinateA; use Moose; extends 'Top'; 1; package SubordinateB; use Moose; extends 'Top'; 1;
and somewhere in some main package I have:
#!/usr/bin/perl -w use strict; use Top. my $obj = Top->new(); | etc.
is there a way from within either Top or one of the subordinate modules to change $obj to be reblessed as either SubordinateA or SubordinateB?

What I'm trying to accomplish is during Top's execution in some sub based on some conditionals instantiate either of the subordinate modules (six actually) and pass that blessed object back to the calling environment.

Does my question make sense?


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Replies are listed 'Best First'.
Re: any way to fill calling namespace with a "use Module"?
by tobyink (Canon) on Feb 23, 2013 at 20:32 UTC

    TIMTOWTDI, but I'd say the "official antlered" way to do it would perhaps be along these lines...

    use v5.14; package Top { use Moose; has letter => ( is => 'ro', isa => Moose::Util::TypeConstraints::enum(['A', 'B']), required => 1, ); sub BUILD { my $self = shift; my $class = "Subordinate" . $self->letter; $class->meta->rebless_instance($self); } } package SubordinateA { use Moose; extends 'Top'; } package SubordinateB { use Moose; extends 'Top'; } print Top->new(letter => "B")->dump;

    Of course, you can just use bless $object => $class to rebless an existing object into a different class. The Top class could do that somewhere in its constructor. (Though the Moose style is to not write constructors, but write BUILD/BUILDARGS methods instead!) However, the rebless_instance method is somewhat smarter - it allows you to pass in additional constructor parameters (in case the subordinate classes defined additional attributes), and checks that the class you're reblessing into is a subclass of the original class.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      Wow! I didn't think of using BUILD that way. That's exactly what I'm looking for. (I'll write an experiment for this) Do you have to invoke use SubordinateA; use SubordinateB; in the invoking environment or ??? I suppose I can work around that...


      Peter L. Berghold -- Unix Professional
      Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
        my $class = "Subordinate" . $self->letter; Class::Load::load_class($class);

        Class::Load is a dependency of Moose, so you're already using it.

        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: any way to fill calling namespace with a "use Module"?
by Corion (Patriarch) on Feb 23, 2013 at 19:46 UTC

    I would restructure the object hierarchy as follows:

    package Top::Base; use Moose; 1; package SubordinateA; use Moose; extends 'Top::Base'; 1; package SubordinateB; use Moose; extends 'Top::Base'; 1;

    ... and then introduce a new "Top" class that doesn't inherit from Moose and whose ->new method is just a factory for the others:

    package Top; sub new { my ($class, %options) = @_; my $impl= "Top::$options{ base }"; $impl->new( %options ); };