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

  • SEARCH
  • Home
  • 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 7991937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:26:03+00:00 2026-06-04T13:26:03+00:00

Here’s the conundrum. I’m using a Moose::Role as an interface, where the concrete classes

  • 0

Here’s the conundrum. I’m using a Moose::Role as an interface, where the concrete classes have to implement the required attribute builders defined by the role. The role also defines some methods which do logic upon the attributes. Here’s a slimmed down version of what I’m trying to do.

package Parent;
use Moose::Role;
requires '_build_permission_level';

has 'permission_level' => (
    is => 'ro',
    isa => Int,
    lazy_build => 1,
);

use constant {
    LEVEL1 = 1,
    LEVEL2 = 2,
    LEVEL3 = 3,
};

sub can_do_action {
    my $self = shift;
    return $self->permission_level() >= LEVEL2;
}

package Child;
use Moose;
with 'Parent';

sub _build_permission_level { return Parent->LEVEL3; }

Obviously I have many child classes, with differing permission levels. Now this works, except it’s horribly inefficient. All Child instances will always have the same permission level, yet I have to instantiate it just to ask if it can perform the action. When running this in bulk 10,000 times, well, you get the picture.

So instead, I’d like to make permission_level a class attribute. Solves the efficiency issue in a Moose-y way. Note how permission_level no longer requires $self.

package Parent;
use Moose::Role;
use MooseX::ClassAttribute;
requires '_build_permission_level';

class_has 'permission_level' => (
    is => 'ro',
    isa => Int,
    builder => '_build_permission_level',
);

use constant {
    LEVEL1 = 1,
    LEVEL2 = 2,
    LEVEL3 = 3,
};

sub can_do_action {
    return permission_level() >= LEVEL2;
}

package Child;
use Moose;
with 'Parent';

sub _build_permission_level { return Parent->LEVEL3; }

This dies with an undefined subroutine error, can’t find Parent::permission_level. So parent has no knowledge of permission_level. Really? I’m confused how it can’t access its own class attribute. I must be missing something really simple. But even more fundamentally, how am I supposed to have Parent provide logic for a class attribute that Child provides?

  • 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-06-04T13:26:04+00:00Added an answer on June 4, 2026 at 1:26 pm

    What the error indicates is that at compile time there exists no sub Parent::permission_level. And this is true: has and class_has are run-time constructs; the sub is not created yet when the call in can_do_action is compiled.

    The real problem is that you’re using the wrong syntax for calling a class method. Rewrite can_do_action thus:

    sub can_do_action {
        my ($class) = @_;
    
        return $class->permission_level >= LEVEL2;
    }
    

    and it should work fine. Unlike a plain function call, a method invocation is not resolved until run time, at which point Parent::permission_level will exist.

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

Sidebar

Related Questions

Here is a complete example. I want to forbid using A::set from objects casted
Here's my scenario: <!-- Normal Control --> <div class=required> <label for=address1>Address line 1</label> <input
Here's the basic setup: I have a thin bar at the top of a
Here is my problem : I have a post controller with the action create.
Here my problem: @Assert\Regex( * pattern=/^[A-Za-z0-9][A-Za-z0-9\]*$/, * groups={creation, creation_logged} * ) I'm using the
Here is an example: I have the generic type called Account. I wish to
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is my code...I have two dimensional matrices A,B. I want to develop the

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.