Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

"individually" override a super class's object methods

by TOD (Friar)
on Nov 13, 2007 at 06:03 UTC ( [id://650441]=perlquestion: print w/replies, xml ) Need Help??

TOD has asked for the wisdom of the Perl Monks concerning the following question:

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.

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

    You need to alter the behavior of the sub. The derived class code looks like:

    package MyChild; use strict; use vars qw/@ISA/; @ISA = qw/MyMother/; sub new { my ($class, @args) = @_; my $self = $class->SUPER::new(@args); bless $self, $class; $self; } sub modified { return 0 if $_[0]->{TYPE} == 1; $_[0]->SUPER::modified (); }

    and the test code then prints:

    1 0

    Perl is environmentally friendly - it saves trees
Re: "individually" override a super class's object methods
by Somni (Friar) on Nov 13, 2007 at 06:53 UTC
    $self->modified = sub { 0 }; produces the error: "Can't modify non-lvalue subroutine call at MyChild.pm line 12"

    This is attempting to call the modified() method and treat it as an lvalue, which requires an lvalue attribute on the subroutine declaration.

    whereas $self::modified = sub { 0 } has no effect.

    This simply assigns an anonymous sub to the scalar $self::modified.

    As GrandFather described, you need to provide actual subroutines. I'm not sure where you came up with this random syntax. You might want to read Beginning Perl's Chapter on object-oriented Perl, perlboot, and perltoot.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://650441]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found