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 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

after I found out (sadly) that there is no easy way to create a
I have a little problem and I don't know how can I fix it
Updated question, see below I'm starting a new project and I would like to
According to the MSDN documentation for the WindowStartupLocation Property : Setting CenterScreen causes a
The iTunes mini-player (to give just one example) supports click-through where the application isn't
I want to implement drag&drop possibilities in my application. Here is my source code:
I'm trying to capture global mouse and keyboard input. LRESULT CALLBACK MouseHookProc(int nCode, WPARAM
I am making a CustomControl based on a ToolStripButton control, I am trying to
I've been trying to do this a number of ways, but none of them
Okay, so at first when i run my win32 program the menu works fine,

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.