hi all,

my question is quite simple: is it possible to individually override a method an object has inherited from its mother class?
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} }
so far for the mother class, here's the child:
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; }
the script for testing:
#!/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";
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.

maybe that question has been put here already sometime, i couldn't find it by a search query. however, if you happen to find an appropriate node, please let me know. (even if it's the link to a manpage. ;-))

many thx in advance
TOD
--------------------------------
masses are the opiate for religion.

In reply to "individually" override a super class's object methods by TOD

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.