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.
In reply to Dynamically setting up inheritence at runtime by hangon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |