1Class::Virtual(3) User Contributed Perl Documentation Class::Virtual(3)
2
3
4
6 Class::Virtual - Base class for virtual base classes.
7
9 package My::Virtual::Idaho;
10 use base qw(Class::Virtual);
11
12 __PACKAGE__->virtual_methods(qw(new foo bar this that));
13
14
15 package My::Private::Idaho;
16 use base qw(My::Virtual::Idaho);
17
18 # Check to make sure My::Private::Idaho implemented everything
19 my @missing = __PACKAGE__->missing_methods;
20 die __PACKAGE__ . ' forgot to implement ' . join ', ', @missing
21 if @missing;
22
23 # If My::Private::Idaho forgot to implement new(), the program will
24 # halt and yell about that.
25 my $idaho = My::Private::Idaho->new;
26
27 # See what methods we're obligated to implement.
28 my @must_implement = __PACKAGE__->virtual_methods;
29
31 THIS MODULE IS DISCOURAGED! Avoid using it for new code. There's
32 nothing wrong with it, but there are better ways to accomplish the same
33 thing. Look into the Moose ecosystem.
34
35 This is a base class for implementing virtual base classes (what some
36 people call an abstract class). Kinda kooky. It allows you to
37 explicitly declare what methods are virtual and that must be
38 implemented by subclasses. This might seem silly, since your program
39 will halt and catch fire when an unimplemented virtual method is hit
40 anyway, but there's some benefits.
41
42 The error message is more informative. Instead of the usual "Can't
43 locate object method" error, you'll get one explaining that a virtual
44 method was left unimplemented.
45
46 Subclass authors can explicitly check to make sure they've implemented
47 all the necessary virtual methods. When used as part of a regression
48 test, it will shield against the virtual method requirements changing
49 out from under the subclass.
50
51 Finally, subclass authors can get an explicit list of everything
52 they're expected to implement.
53
54 Doesn't hurt and it doesn't slow you down.
55
56 Methods
57 virtual_methods
58 Virtual::Class->virtual_methods(@virtual_methods);
59 my @must_implement = Sub::Class->virtual_methods;
60
61 This is an accessor to the list of virtual_methods. Virtual base
62 classes will declare their list of virtual methods. Subclasses
63 will look at them. Once the virtual methods are set they cannot be
64 undone.
65
66 missing_methods
67 my @missing_methods = Sub::Class->missing_methods;
68
69 Returns a list of methods Sub::Class has not yet implemented.
70
72 Autoloaded methods are currently not recognized. I have no idea how to
73 solve this.
74
76 Michael G Schwern <schwern@pobox.com>
77
79 Copyright 2000-2015 Michael G Schwern
80
81 This program is free software; you can redistribute it and/or modify it
82 under the same terms as Perl itself.
83
84 See <http://dev.perl.org/licenses/>
85
87 Class::Virtually::Abstract
88
89
90
91perl v5.32.1 2021-01-27 Class::Virtual(3)