Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Class-InsideOut CPAN distribution.

Report information
The Basics
Id: 26414
Status: resolved
Priority: 0/
Queue: Class-InsideOut

People
Owner: Nobody in particular
Requestors: taro-nishino [...] sam.hi-ho.ne.jp
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: (no value)
Fixed in: (no value)



Subject: can't use the SUPER pseudo-class
Class-InsideOut-1.06 Unfortunately, the following code doesn't work well. In short, the field "parenttype" in this code is undef. Of course, if the statement "$self->SUPER::new()" is replaced with "TestA->new()", then the code works well. However, in this case using the SUPER pseudo-class isn't suitable, the use of "use base qw(TestA)" is meaningless at all. It's no longer "inheritance". package TestA; use strict; use warnings; use Class::InsideOut qw( :std ); { public type => my %type; sub new { my $class = shift; my $self = ref $class || $class; $self = register($self); $type{id $self} = 'A'; return $self; } } package TestB; use strict; use warnings; use Class::InsideOut qw( :std ); use base qw(TestA); use Smart::Comments; { public parenttype => my %parenttype; public type => my %type; sub new { my $class = shift; my $self = ref $class || $class; $self = register($self); my $testa = $self->SUPER::new(); # Must "$self->SUPER::new()" be replaced with "TestA->new()" ? $parenttype{id $self} = $testa->type(); $type{id $self} = 'B'; return $self; } } package main; use strict; use warnings; my $testb = TestB->new(); print $testb->parenttype(), "\n"; print $testb->type(), "\n"; __END__
Subject: can't use the SUPER pseudo-class (not a bug)
On Mon Apr 16 16:10:45 2007, taro-nishino@sam.hi-ho.ne.jp wrote: Show quoted text
> Class-InsideOut-1.06 > > Unfortunately, the following code doesn't work well. In short, the > field "parenttype" in this code is undef. > Of course, if the statement "$self->SUPER::new()" is replaced > with "TestA->new()", then the code works well. > However, in this case using the SUPER pseudo-class isn't suitable, > the use of "use base qw(TestA)" is meaningless at all. It's no > longer "inheritance".
SUPER doesn't really do what you seem to think it does. $self->SUPER::new() is equivalent to SUPER::new( $self ) Thus, following the logic you use in your constructor, the class you get back is still TestB, not TestA, but meanwhile, you're setting the property that would be returned from TestA's type accessor, which is hidden by TestB's type accessor. You are blending constructors and initializers and that is complicated work, particularly since you define "type" in both classes. You can always tell what the parent classes are by examining @ISA, so I'm not sure what you're trying to accomplish. Here are some options that might help you: (A) $parenttype{ id $self } = $testa->SUPER::type(); This way, you call the superclass accessor, which gives you the type set in the superclass constructor. (B) $self = register( __PACKAGE__ ); Hard-code the package into the constructor, rather than determining it from an object passed to the constructor (but then every class needs to have its own constructor). (C) $self->type('A'); Initialize in the superclass using methods, so you always set the publicly "visible" type property (TestB's in your case). (D) delete the type property from TestB and use "$self->type('B')" Keep type only in the superclass and access it via methods in the subclass.
From: taro-nishino [...] sam.hi-ho.ne.jp
Thank you for advising me. I'd like to rethink my problem, considering your advice. Anyway, it's unchangeable that your module, Class::InsideOut is reliable and usable.