The procedure documented in the 'fields' module for creating a subclass
is not transitive. Otherwise, if C subclasses B subclasses A, it is
necessary in C to write:
$self = $self->SUPER::new(@_);
or the fields do not get initialized. In B we can write:
$self->SUPER::new(@_);
as documented. The reason that this fails in C is that B assigns
to $self without checking if it is already a reference.
To make the subclassing method is transitive, it is sufficient to
change this line:
my $self = fields::new($class);
to this:
my $self = ref($class) ? $class : fields::new($class);
and then the line:
$self->SUPER::new(@_);
works in both B and C.
Here is the context from 'perldoc fields' (white-space edited to fit):
# subclassing
{
package Bar;
use base 'Foo';
use fields qw(baz _Bar_private); # not shared with Foo
sub new {
my $class = shift;
my $self = fields::new($class);
$self->SUPER::new(); # init base fields
$self->{baz} = 10; # init own fields
$self->{_Bar_private} = "this is Bar's secret";
return $self;
}
}