TOD has asked for the wisdom of the Perl Monks concerning the following question:
so far for the mother class, here's the child:package MyMother; use strict; use vars qw/@ISA/; use Exporter; @ISA = qw/Exporter/; sub new { my ($class, @args) = @_; my $self = { MODIFIED => 0, TYPE => $args[0], }; bless $self, $class } sub modified { $_[0]->{MODIFIED} }
the script for testing:package MyChild; use strict; use vars qw/@ISA/; use MyMother; @ISA = qw/MyMother/; sub new { my ($class, @args) = @_; my $self = $class->SUPER::new(@args); bless $self, $class; if ($self->{TYPE} == 1) { $self->modified = sub { 0 }; } $self; }
as you can see, my intention is to make some certain instances always return the false value in a call to $obj->modified. the thing is that $self->modified = sub { 0 }; produces the error: "Can't modify non-lvalue subroutine call at MyChild.pm line 12", whereas $self::modified = sub { 0 } has no effect.#!/usr/bin/perl -wd use MyChild; my $ch = MyChild->new(2); $ch->{MODIFIED} = 1; print $ch->modified."\n"; $ch = MyChild->new(1); $ch->{MODIFIED} = 1; print $ch->modified."\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: "individually" override a super class's object methods
by GrandFather (Saint) on Nov 13, 2007 at 06:32 UTC | |
|
Re: "individually" override a super class's object methods
by Somni (Friar) on Nov 13, 2007 at 06:53 UTC |