hangon has asked for the wisdom of the Perl Monks concerning the following question:
I want to inherit a module based on a parameter given to the constructor. The simplified example below should illustrate what I'm trying to do, but it doesn't work (I assume because @ISA needs to be global). In the real module, the constructor will receive a filename, then inherit another module based on the file's extension.
Is it possible to make something like this work? Or are there better alternatives? Thanks.
use strict; use warnings; my $ob1 = foo->new('alpha'); my $ob2 = foo->new('beta'); $ob1->write; $ob2->write; package foo; sub new{ my $class = shift; our @ISA = (shift); return bless {}, $class; } sub write{ my $self = shift; print $self->read; } package alpha; sub read{ return "Alpha\n"; } package beta; sub read{ return "Beta\n"; }
Desired output:
Alpha Beta
Update: Thanks for the help everyone. I think I've got a handle on this now.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dynamically setting up inheritence at runtime
by kyle (Abbot) on Jan 26, 2009 at 20:14 UTC | |
|
Re: Dynamically setting up inheritence at runtime
by ikegami (Patriarch) on Jan 26, 2009 at 20:09 UTC | |
by hangon (Deacon) on Jan 26, 2009 at 21:17 UTC | |
|
Re: Dynamically setting up inheritence at runtime
by kennethk (Abbot) on Jan 26, 2009 at 19:30 UTC | |
|
Re: Dynamically setting up inheritence at runtime
by JavaFan (Canon) on Jan 26, 2009 at 20:45 UTC | |
|
Re: Dynamically setting up inheritence at runtime
by ikegami (Patriarch) on Jan 26, 2009 at 19:55 UTC | |
|
Re: Dynamically setting up inheritence at runtime
by DrHyde (Prior) on Jan 28, 2009 at 10:33 UTC |