Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6007703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:43:08+00:00 2026-05-23T01:43:08+00:00

I have a base class and a dozen derived classes. All but one derived

  • 0

I have a base class and a dozen derived classes. All but one derived classes require an attribute named key. So I could add it to eleven derived classes and leave the twelfth one alone.

However, laziness being what it is, I’d like to add the attribute to the base class, thus avoiding repeating the declaration eleven times and adding what I consider is consistency and simplicity.

Now, that poses a problem for the one class that does not require the key attribute. Note that there is no harm if this class has this attribute, but it does not require it.

My idea has been to resolve this by using a flag method is_strict, which would be called from BUILDARGS to decide whether the key is required or not. Here’s a simple script to illustrate this (okay, I inverted the notion, the key attribute is required in only one case (instead of in all but one cases), but the problem remains affected by this inversion):

#!perl
package Bla;
use Moose;
use Carp ();
has grop => is => 'ro', isa => 'Str'; # optional
has key  => is => 'ro', isa => 'Int'; # required in all but one cases
# required => should depend on $class->is_strict;
sub is_strict { 0 } # not strict by default as per this base class
# imagine a bunch of other stuff here shared by all derived classes
around BUILDARGS => sub {
    my $orig = shift;
    my $class = shift;
    my $args = @_ == 1 ? shift : { @_ };
    Carp::croak 'key missing'
        if not exists $args->{key}
        and $class->is_strict;
    return $class->$orig( @_ );
};
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Eins;
use Moose; extends 'Bla';
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Zwei;
use Moose; extends 'Bla';
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Drei;
use Moose; extends 'Bla';
override is_strict => sub { 1 }; # but here it is required
no Moose; __PACKAGE__->meta->make_immutable;

package main;
use Test::More;
use Test::Exception;
lives_ok  { Bla::Eins->new };
lives_ok  { Bla::Zwei->new };
throws_ok { Bla::Drei->new } qr/key missing/;
lives_ok  { Bla::Drei->new( key => 99 ) };
done_testing;

This works, but is there a better way to achieve what I want?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:43:09+00:00Added an answer on May 23, 2026 at 1:43 am

    Okay, I’ve been a little dumb for not having tried the most obvious solution, just override the attribute definition in the derived class where the requiredness differs from the default. Here we go:

    #!perl
    package Bla;
    use Moose;
    use Carp ();
    has grop => is => 'ro', isa => 'Str'; # optional
    has key  => is => 'ro', isa => 'Int'; # required in all but one cases
    # imagine a bunch of other stuff here shared by all derived classes
    no Moose; __PACKAGE__->meta->make_immutable;
    
    package Bla::Eins;
    use Moose; extends 'Bla';
    no Moose; __PACKAGE__->meta->make_immutable;
    
    package Bla::Zwei;
    use Moose; extends 'Bla';
    no Moose; __PACKAGE__->meta->make_immutable;
    
    package Bla::Drei;
    use Moose; extends 'Bla';
    # prefix an attribute you're overriding with a "+" sign
    has '+key' => is => 'ro', isa => 'Int', required => 1;
    no Moose; __PACKAGE__->meta->make_immutable;
    
    package main;
    use Test::More;
    use Test::Exception;
    lives_ok  { Bla::Eins->new };
    lives_ok  { Bla::Zwei->new };
    throws_ok { Bla::Drei->new } qr/\bkey\b.*\brequired\b/;
    lives_ok  { Bla::Drei->new( key => 99 ) };
    done_testing;
    

    Still, I’m thankful for all feedback as this Mooseland is less well chartered than object systems in other languages.

    Ah, and I’ve seen you’re supposed to prefix an attribute definition that you’re overriding with a +, so Moose will pitifully croak if the referenced attribute isn’t found in a parent class, adding security and consistency to your code. I’ve updated my sample code accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class vehicle and some children classes like car, motorbike etc..
I have a base class and derived class. If I'm creating an object of
I have a base class DomainObject for all my business objects I am using
I have a base class object array into which I have typecasted many different
I have a base class with an optional virtual function class Base { virtual
I have a base class that represents a database test in TestNG, and I
I have a base class with a property which (the get method) I want
I have a base class that has a private static member: class Base {
I have a base class in which I want to specify the methods a
I have a base class with a virtual method, and multiple subclasses that override

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.