I don't see why that would be good practice.
Let's create a telephone class:
package Example::Phone {
use Moo;
has number => (is => 'ro', required => 1);
sub call { ... }
}
Now let's use it:
use Example::Phone;
my $dad = Example::Phone->new(number => '+44 123 456 7890');
say "Dad's number is ", $dad->number;
$dad->call();
The object is immutable, in that once it's been instantiated, you can't use its public API to alter the number string.
But that shouldn't stop us from subclassing it:
package Example::Phone::Mobile {
use Moo;
extends 'Example::Phone';
sub send_sms { ... }
}
It doesn't in any way compromise the immutability of the parent class. |