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

How to delete a method in a subclass?

by Anonymous Monk
on Dec 17, 2004 at 22:22 UTC ( #415798=perlquestion: print w/replies, xml ) Need Help??

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

If I am subclassing a module and I don't want some methods of the superclass to be accessible, how do I get rid of them? Can I delete them or should I just redefine them to call die? I don't have Damian's book handy...

Replies are listed 'Best First'.
Re: How to delete a method in a subclass?
by gaal (Parson) on Dec 17, 2004 at 22:47 UTC
    What do you want to happen if someone calls such a method?

    Anyway, yeah -- override the method in the subclass. Have it throw an exception, or do nothing. If you have lots of methods like that (then you should rethink your design, but) you can do something like

    sub meth1 { obsoleted } sub meth2 { obsoleted } sub meth3 {} # do nothing, quietly. # [...] sub obsoleted { require Carp; Carp::croak "obsoleted method called"; # exception, with stack tra +ce # or, just complain but go on with the show. Carp::cluck "obsoleted method called"; }

      I'm a fan of keeping the parenthesis for user-defined subroutines, and you give an example of why.

      Your methods will have the string "obsoleted" in them. With warnings enabled a warning will be issued. With strict enabled it'll be a fatal error.

      This is because when the methods are defined there is no &obsoleted subroutine and you don't indicate it's a subroutine in any way.

      Defining &obsoleted above the methods would solve this, but personally I'd just use parenthesis and not worry about where I define subroutines.

      ihb

      See perltoc if you don't know which perldoc to read!
      Read argumentation in its context!

        Yes, that's a good point++.
Re: How to delete a method in a subclass?
by kvale (Monsignor) on Dec 17, 2004 at 22:37 UTC
    Reaching into a superclass to mess with the symbol table is considered rude and is almost always bad programming practice. It is much preferable to override the method with your warning.

    -Mark

Re: How to delete a method in a subclass?
by borisz (Canon) on Dec 17, 2004 at 22:38 UTC
    You can call die or whatever. But I guess subclass is not what you should do. You should create a new class and delegate the supported functions to a instance of your other class.
    Boris
Re: How to delete a method in a subclass?
by Arunbear (Prior) on Dec 17, 2004 at 22:45 UTC
    You can't delete them because they don't exist in the package that defines your subclass.
Re: How to delete a method in a subclass?
by Anonymous Monk on Dec 18, 2004 at 05:25 UTC
    I guess you just fire up the old text editor and delete the lines of code that define the methods.
      Respectfully, I don't think that this is a very good idea.

      To be clear, if you delete the code in the base module, then when you or someone else goes to use that module in the future, it won't work.

      Far better to redefine the offending subs.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://415798]
Approved by Arunbear
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2020-08-14 23:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (77 votes). Check out past polls.

    Notices?