[prog] perl interfaces

Mary mary-linuxchix at puzzling.org
Thu Apr 15 10:35:18 EST 2004

On Wed, Apr 14, 2004, Caroline Johnston wrote:
> Any pitfalls I should know about? Any other solutions I ought to take
> a look at?

Damian Conway, in "Object Oriented Perl" (section 6.2.5 in my edition)
suggests the following boiler-plate:

    my ($self) = @_;
    my $object_class = ref($self);
    my ($file, $line, $method) = (caller(1))[1..3];
    my $loc = "at $file, line $line\n";
    die "call to abstract method $(method) $loc";

and then for any method you want to be abstract, define it in the
interface class as:

sub abstractMethod { ABSTRACT METHOD @_ };

If abstractMethod is ever called, then it will execute the boilerplate
and the program will die with a nice "call to method abstractMethod at
file FILE line LINE" message.

The caller(1) call gets the details of the call to abstractMethod
including file and line number. (It's caller(1) rather than caller(0)
because we don't want it to say "call to method ABSTRACT at
file FILE line LINE".)


More information about the Programming mailing list