jaa has asked for the wisdom of the Perl Monks concerning the following question:

It's been a while since I hit the keys on my codeboard - so apologies if ya'all know all this anyway.

I've recently discovered Object::Pad - and love it. Fingers crossed for some future OO goodness in CORe.

I've kind of forgotten where to ask Perl questions, and posted one on Discord->#perl:

A question on Object::Pad and virtual parent classes.

I want to class Parent :does(RoleA) :does(RoleB) and have class ChildA isa:(Parent); class ChildB isa:(Parent) etc. without having to implement the RoleA and RoleB requires methods inside Parent - in other words, Parent is a virtual class - one that would never be instantiated.

If I do this, it appears that the :does() are evaluated on Parent at compilation rather than instantiation of ChildA or ChildB which actually do implement the requires.

Is there a way to do virtual parent classes?

Kudos to gordonfish for suggesting using role to do this.

So as illustration, you can use role as follows to compose virtual classes.

#!/usr/bin/perl #----------------------------------------------------------------- +------------- use v5.32; use Object::Pad; role Role::FeederThings { requires earnDosh; requires buyKFC; } role Role::TaxiDriver { requires waitForever; requires driveForMiles; } role Role::DadThings :does(Role::FeederThings) :does(Role::TaxiDriver) { } class Parent :does(Role::DadThings) { }

Which results in:

Class Parent does not provide a required method named 'earnDosh' at +./,t1.pl line 25.

Just sharing...

0.02$AU, Jeff.

Replies are listed 'Best First'.
Re: Object::Pad - Virtual Classes can be composed using Roles.
by kcott (Archbishop) on Jan 08, 2022 at 02:47 UTC

    G'day Jeff,

    "I've kind of forgotten where to ask Perl questions, ..."

    On this site, "Seekers of Perl Wisdom" is where you post questions; "Meditations" is more for discussion and commentary. The top of both of those pages has more details about this; other sections (e.g. "Perl Monks Discussion") have equivalent information regarding appropriate usage.

    Your post contains both a discussion and a question; however, as you've emboldened the question, I considered "Seekers of Perl Wisdom" to be the more appropriate section and have moved your post there.

    Beyond that, I had no problems with your post and have both approved and upvoted it.

    Minor update: LanX beat me to the approval.

    — Ken

      Fab, thanks && noted.
Re: Object::Pad - Virtual Classes can be composed using Roles.
by jaa (Friar) on Jan 09, 2022 at 00:58 UTC