in reply to Interfaces

You could create an abstract class where each method prints an error, like so:
package AbstractClass; sub new { my($class) = shift; bless { "classVar1" => undef, "classVar2" => undef }, $class; } sub methodOne { print "Error: Use of undefined Abstract Method\n"; } sub methodTwo { print "Error: Use of undefined Abstract Method\n"; }
and then override the methods in the subclasses:
package SomeObject; @ISA = ("AbstractClass"); sub methodOne { print "Doing the real function\n"; }

that's very basic code...would it do something like what you want?

Replies are listed 'Best First'.
RE: Re: Interfaces
by btrott (Parson) on Jun 07, 2000 at 03:16 UTC
    Since you're writing a bunch of subroutines that do the same thing, you can be lazy and just alias them:
    sub cant_do_that { my $self = shift; print "Error: Use of undefined Abstract Method by $self.\n"; } use subs qw/methodOne methodTwo/; *methodOne = \&cant_do_that; *methodTwo = \&cant_do_that;
      Thus saith btrott:
      sub cant_do_that { my $self = shift; print "Error: Use of undefined Abstract Method by $self.\n"; } use subs qw/methodOne methodTwo/; *methodOne = \&cant_do_that; *methodTwo = \&cant_do_that;
      I'd simplify the whole lot of that by doing what use subs does directly:
      BEGIN { for my $fakir (qw(methodOne methodTwo)) { *$fakir = sub { die "undef abstract method $fakir used by ".(shift +); } } }
      No fuss, no muss, and you even get a distinguishing mark.

      -- Randal L. Schwartz, Perl hacker

        merlyn:
        BEGIN { for my $fakir (qw(methodOne methodTwo)) { *$fakir = sub { die "undef abstract method $fakir used by ".(shift +); } } }
        causes an error with use strict; (Can't use string ("foo") as a symbol ref) and i needed to add
        no strict 'refs';
        inside the BEGIN:...is there a better way?

        --
        michael d. ivey, ivey@gweezlebur.com

RE: Re: Interfaces
by chromatic (Archbishop) on Jun 07, 2000 at 03:01 UTC
    I've done something exactly like that. There is one additional bit in my code, though.
    sub methodOne { my $self = shift; print "Error: Use of undefined Abstract Method by $self\n"; }
    That way, you know exactly which subclass isn't overriding it.