| Use Case | Command | Description |
|---|---|---|
| Declare virtual methods | use base qw(Class::Virtually::Abstract); __PACKAGE__->virtual_methods(qw(new foo bar this that)); | Define a virtual class that forces subclasses to implement listed methods. |
| Implement subclass | package My::Private::Idaho; use base qw(My::Virtual::Idaho); sub foo { ... } | Create a concrete subclass; must implement all virtual methods or compilation fails. |
| Use subclass (compilation check) | use My::Private::Idaho; | Triggers compile-time check — if any virtual method is missing, the use statement fails. |
package My::Virtual::Idaho;
use base qw(Class::Virtually::Abstract);
__PACKAGE__->virtual_methods(qw(new foo bar this that));
package My::Private::Idaho;
use base qw(My::Virtual::Idaho);
sub new { ... }
sub foo { ... }
sub bar { ... }
sub this { ... }
# oops, forgot to implement that()!! Whatever will happen?!
# Meanwhile, in another piece of code!
# KA-BLAM! My::Private::Idaho fails to compile because it didn't
# fully implement My::Virtual::Idaho.
use My::Private::Idaho;
This subclass of Class::Virtual provides compile-time enforcement. That means subclasses of your virtual class are required to implement all virtual methods or else it will not compile.
Because this relies on import() it is important that your classes are used instead of required. This is a problem, and I'm trying to figure a way around it.
Also, if a subclass defines its own import() routine (I've done it) Class::Virtually::Abstract's compile-time checking is defeated.
Got to think of a better way to do this besides import().
Original idea and code from Ben Tilly's AbstractClass
http://www.perlmonks.org/index.pl?node_id=44300&lastnode_id=45341
Embraced and Extended by Michael G Schwern <schwern AT pobox.com>
Generated by phpman v4.9.29 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-22 01:25 @2600:1f28:365:80b0:d8a5:c3c6:bf94:28c0
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format