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 3288182
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:34:42+00:00 2026-05-17T20:34:42+00:00

How should I define a Moose object subroutine after its initialization? I’m writing an

  • 0

How should I define a Moose object subroutine after its initialization?

I’m writing an object module using Moose and I plan to serialize (nstore) the created objects.

Examine the following (simplified!) example:

package MyObj 0.001;

use Moose;
use namespace::autoclean;

has 'size' => (
 is       => 'ro',
 isa      => 'Int',
 required => 1,
);

sub some_sub {
 my ($self, @more) = @_;
 if ($self->size() < 100) # do something;
 elsif (($self->size() < 500)) # do something else;
 elsif (($self->size() < 7500)) # do something else;
 # ...
}

1;

some_sub acts differently depending on size. Since size is read-only, it remains constant after the object has been initialized.

So, assuming I call some_sub zillion times, it’s a pity that I have to go through all the ifs each time.

I’d better do this once after the object has been initialized, then set some_sub to be a simpler function with noifs at all.

But… how can I do that?

UPDATE

Perhaps I should add a lazy attribute of type subref that will hold a reference to the chosen subroutine. some_sub will then simply call $self->chosen_sub->(@_). What do you think?

  • 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-17T20:34:42+00:00Added an answer on May 17, 2026 at 8:34 pm
    has calculation_method => (is => 'ro', lazy_build => 1, init_arg => undef);
    
    sub _build_calculation_method {
        my $self = shift;
        return '_calculate_small'  if $self->size < 100;
        return '_calculate_medium' if $self->size < 500;
        return '_calculate_large'  if $self->size < 7500;
        return '_calculate_enormous';
    }
    
    sub _calculate_small  { ... }
    sub _calculate_medium { ... }
    # etc.
    
    sub calculate {
        my $self = shift;
        my $method = $self->calculation_method;
        return $self->$method(@_);
    }
    

    As a bonus, calculation_method is now serializable too.

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

Sidebar

Related Questions

I have a Moose object module that should accept a relatively large data structure
Should developers avoid using continue in C# or its equivalent in other languages to
I read that you should define your JavaScript functions in the <head> tag, but
I have a very thin interface that's going to define one method. Should I
Should I still be using tables anyway? The table code I'd be replacing is:
I'm beginning Scala. Am I correct understanding that I should define a class as
How should I define large strings in VBA? Is there a better way than
I am using a 3rd-party rotator object, which is providing a smooth, random rotation
I should define a function overlapping() that takes two lists and returns True if
Checkstyle says: Class should define a constructor. PMD says: Avoid unnecessary constructors - 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.