Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: any way to fill calling namespace with a "use Module"?

by tobyink (Abbot)
on Feb 23, 2013 at 20:32 UTC ( #1020341=note: print w/ replies, xml ) Need Help??


in reply to any way to fill calling namespace with a "use Module"?

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


Comment on Re: any way to fill calling namespace with a "use Module"?
Select or Download Code
Re^2: any way to fill calling namespace with a "use Module"?
by blue_cowdawg (Prior) on Feb 23, 2013 at 21:56 UTC

    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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2014-07-25 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (173 votes), past polls