in reply to Inherit only few methods from a parent class

You can't by using a language construct. But since it's Perl, you can do:
package ChildClass; use ParentClass; our @ISA = qw[ChildClass]; sub risky_method1 {die "You're not allowed to call this"} sub risky_method2 {die "You're not allowed to call this either"}
Repeat for each method you don't want to inherit.

Replies are listed 'Best First'.
Re^2: Inherit only few methods from a parent class
by Anonymous Monk on Jul 13, 2009 at 15:37 UTC
    Thanks much for everydody's reply. I impliment something like JavaFan recommends and it works..
    Currently I am trying to avoid the following for like 90 methods..
    sub risky_method1 {die "You're not allowed to call this"}
    sub risky_method2 {die "You're not allowed to call this either"}

    How can I impliment that? I heard about closures and AUTOLOAD in perl , Do you guys know which one is best?
    Any examples

    Thanks,
    Shijumic
      AUTOLOAD isn't going to work as AUTOLOAD will only be called if the method cannot be found.

      You could created the methods in a loop from a BEGIN statement. What I probably would do is write a one liner which generates the subs, which I'd then concatenate to the file.

        Would you please give me an example..
        Sorry I am new to Perl!!

        Thanks,
        Shijumic
Re^2: Inherit only few methods from a parent class
by afoken (Chancellor) on Jul 14, 2009 at 18:52 UTC

    I don't like that construction. If the "risky" methods are used in the parent class, the inheriting child class may die unexpectedly:

    #!/usr/bin/perl -w use strict; package ParentClass; sub new { my $class=shift; return bless {},$class; } sub run { my $self=shift; $self->hello(); $self->risky(); $self->world(); } sub hello { print "*** Hello "; } sub risky { print "risky "; } sub world { print "world! ***\n"; } package ChildClass; our @ISA=qw(ParentClass); sub risky { die "You're not allowed to call risky()"; } package main; $|=1; print "ParentClass:\n"; my $p=ParentClass->new(); $p->run(); print "ChildClass:\n"; my $c=ChildClass->new(); $c->run();

    The only clean solution is to have a common base class without the "risky" methods:

    #!/usr/bin/perl -w use strict; package BaseClass; sub new { my $class=shift; return bless {},$class; } sub hello { print "*** Hello "; } sub world { print "world! ***\n"; } package FormerParentClass; our @ISA=qw(BaseClass); sub risky { print "risky "; } sub run { my $self=shift; $self->hello(); $self->risky(); $self->world(); } package FormerChildClass; our @ISA=qw(BaseClass); sub run { my $self=shift; $self->hello(); $self->world(); } package main; $|=1; print "FormerParentClass:\n"; my $p=FormerParentClass->new(); $p->run(); print "FormerChildClass:\n"; my $c=FormerChildClass->new(); $c->run();

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      If the "risky" methods are used in the parent class, the inheriting child class may die unexpectedly
      I agree with everything above, except the word unexpectedly. Yes, it will die. On purpose. Because the OP does not want those methods be callable. And if the Parent class calls those subs as methods, it's calling them on objects of type Child. Which the OP doesn't want. Hence death.

        Let's say "dies unexpectedly for the OP". I had and still have the impression that the OP hasn't completely understood inheritance yet.

        If the OP would show us some code, and explain why (s)he thinks that some methods in the parent class are risky, we could do much more than just guessing.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)