Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Interfaces

by ivey (Beadle)
on Jun 06, 2000 at 23:54 UTC ( [id://16719]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://16719]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-07-21 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.