toma has asked for the wisdom of the Perl Monks concerning the following question:
My goal is to add new functionality to Bit::Vector
without changing its source code.
My strategy has been to make my own class that inherits
from Bit::Vector,
but it is resisting my strategy.
How can I make inheritance work with Bit::Vector? If inheritance won't work, what approach should I use?
The error message I get is 'Modification of a read-only value attempted' when I try to re-bless the Bit::Vector. If I don't re-bless, I get a Bit::Vector where I expect an LFSR object.
Here is my attempt so far:
The stripped-down calling program:
The stripped-down module:use strict; use warnings; use diagnostics; use LFSR; my $nbits = 127; my $t = new LFSR($nbits); # Inherited constructor print ref($t),"\n"; # Shows it is a Bit::Vector, not an LFSR # More proof that $t is a Bit::Vector print $t->isa('Bit::Vector'),"\n"; print $t->isa('LFSR'),"\n"; # This line: # bless $t, "LFSR"; # Causes this error: # 'Modification of a read-only value attempted'
I am running perl version 5.6.1.package LFSR; use strict; use warnings; use diagnostics; use Bit::Vector; our @ISA = qw(Bit::Vector); # This also causes an error at the bless statement, # so I have it commented out. =pod sub new { my ($class, $nbits)= @_; my $self= new Bit::Vector($nbits); bless $self, $class; return $self; } =cut sub mine { my $self= shift; my $class= ref($self) || $self; print "Hello\n"; } 1;
Thanks!
-toma
It should work perfectly the first time! - toma
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How can I inherit from Bit::Vector?
by Fastolfe (Vicar) on Nov 05, 2001 at 01:48 UTC | |
by Fastolfe (Vicar) on Nov 05, 2001 at 02:34 UTC | |
by toma (Vicar) on Nov 05, 2001 at 04:17 UTC | |
by Fastolfe (Vicar) on Nov 05, 2001 at 04:21 UTC | |
Re: How can I inherit from Bit::Vector?
by alien_life_form (Pilgrim) on Nov 05, 2001 at 06:08 UTC | |
by tye (Sage) on Nov 05, 2001 at 14:09 UTC | |
Re: How can I inherit from Bit::Vector?
by alien_life_form (Pilgrim) on Nov 05, 2001 at 21:02 UTC |
Back to
Seekers of Perl Wisdom