oop - How to make a member of a class an object in perl -
hello question of oo programming in perl. want have 2 objects , b, , contains fellow member variable of type b. did tests seems doesn't work. idea?
a.pmpackage a; sub new{ $self = {}; $b = shift; $self->{b} = $b; bless $self; homecoming $self; } sub doa{ $self = shift; print "a\n"; $self->{b}->dob; } 1;
b.pm package b; sub new { $self = {}; bless $self; homecoming $self; } sub dob{ $self = shift; print "b\n"; } 1;
test.pl use a; utilize b; $b = b->new; $a = a->new($b); $a->doa;
when ran this, shows:
a can't locate object method "dob" via bundle "a" @ a.pm line 16.
you forgot method's first parameter. first parameter of method invocant.
sub new { ($class, $b) = @_; $self = {}; $self->{b} = $b; homecoming bless($self, $class); }
i bless
first, though
sub new { ($class, ...) = @_; $self = bless({}, $class); $self->{attribute} = ...; homecoming $self; }
because it's more consistent constructor of derived class.
sub new { ($class, ...) = @_; $self = $class->super::new(...); $self->{attribute} = ...; homecoming $self; }
perl oop
No comments:
Post a Comment