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:
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'
The stripped-down module:
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;
I am running perl version 5.6.1.
Thanks!
-toma
It should work perfectly the first time! - toma
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.