Class:...stract(3pm)
π Class::Virtually::Abstract π
okay bestie, so this is a page from the User Contributed Perl Documentation β it's Class:...stract(3pm) (yes the name is literally truncated like that, the header just do be like that sometimes π« ) and it is so much better than its name suggests.
Short version: Class::Virtually::Abstract is compile-time enforcement for Class::Virtual. That's the whole pitch. That's the vibe. β¨
πΈ NAME πΈ
Class::Virtually::Abstract β compile-time enforcement of Class::Virtual π
π SYNOPSIS π
like, here's the whole situation:
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;
not the # oops, forgot to implement that()!! Whatever will happen?! followed by KA-BLAM! π the original author really said "let me narrate my own plot twist" and honestly? iconic. π
π¦ DESCRIPTION π¦
so basically β this is a subclass of Class::Virtual, and what it adds is compile-time enforcement. that means: if your class is a subclass of your virtual class, you are required to implement all the virtual methods. don't do it? it will not compile. periodt. πβ¨
no runtime surprises. no "oh I guess the method just doesn't exist" at 3am. the compiler said nope and it meant it.
π BUGS and CAVEATS π
ngl bestie, this part is the "read the whole thing" part. π«Ά
- because this whole thing relies on
import(), it is important that your classes areused instead ofrequired. that's a problem, and the author is trying to figure out a way around it. π - also, if a subclass defines its own
import()routine (and yeah, the author has done it π) thenClass::Virtually::Abstract's compile-time checking gets defeated. just gone. bye. - the author's own words: got to think of a better way to do this besides
import().
so real talk: this is a known-weak spot, not a hidden feature. don't build your whole house on it and then act surprised, iykyk. ποΈ
π AUTHOR π
- 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@pobox.com>β the way he "embraced and extended" it, we love a glow-up π«Ά
π SEE ALSO π
Class::Virtualβ the parent, the blueprint, the main character this whole page is vibing off of π
and that's the whole page bestie!! π¦ this beauty shipped as part of the User Contributed Perl Documentation, marked perl v5.34.0, dated 2022-06-12. go implement your virtual methods. all of them. no skipping. mwah π β¨
π¦ where this page comes from
- debian bookworm: libclass-virtual-perl 0.08-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libclass-virtual-perl 0.08-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libclass-virtual-perl 0.08-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libclass-virtual-perl 0.08-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libclass-virtual-perl 0.08-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms