Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This just hit me as The Right Way To Do This.
package Foo; use Carp; # Time passes # Import method that checks existence of abstract methods in subclasse +s. sub import { my $pkg = shift; return if $pkg eq __PACKAGE__; foreach my $meth ( qw(foo bar) ) { $pkg->can($meth) or croak("Class $pkg does not define method $meth +"); } $pkg->SUPER::import(@_); }
The import method will do nothing if you import the base class, but will die a horrible flaming death if you ever use or import a class that inherits from it which does not implement the abstract methods you want.

Note that the base class cannot (with this method) define the abstract methods itself, it just lists them. (They could be in an array, etc.)

UPDATE
I added inheritance so one abstract class can inherit from another. This breaks on multiple inheritance because Perl's SUPER mechanism doesn't handle this cleanly.

UPDATE 2
tye pointed out in the chatter that I should document the fact that if you define your own import method and don't call the SUPER::import method inside of it, then you will break this mechanism.

UPDATE 3
I took the snippet above and turned it into a useful module which may be found at AbstractClass.


In reply to Abstract class methods by tilly
in thread Interfaces in Perl? by gregorovius

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-23 05:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found