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

The Archive Base Latest Questions

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

Using Moose, is it possible to create a builder that builds multiple attributes at

  • 0

Using Moose, is it possible to create a builder that builds multiple attributes at once?

I have a project in which the object has several ‘sets’ of fields – if any member of the set is requested, I want to go ahead and populate them all. My assumption is that if I need the name, I’ll also need the birthdate, and since they’re in the same table, it’s faster to get both in one query.

I’m not sure if my question is clear enough, but hopefully some sample code will make it clear.

What I have:

Package WidgetPerson;
use Moose;

has id => (is => 'ro', isa => 'Int' );
has name => (is => 'ro', lazy => 1, builder => '_build_name');
has birthdate => (is => 'ro', lazy => 1, builder => '_build_birthdate');
has address => (is => 'ro', lazy => 1, builder => '_build_address');

sub _build_name {
 my $self = shift;
 my ($name) = $dbh->selectrow_array("SELECT name FROM people WHERE id = ?", {}, $self->id);
 return $name;
}
sub _build_birthdate {
 my $self = shift;
 my ($date) = $dbh->selectrow_array("SELECT birthdate FROM people WHERE id = ?", {}, $self->id);
 return $date;
}
sub _build_address {
 my $self = shift;
 my ($date) = $dbh->selectrow_array("SELECT address FROM addresses WHERE person_id = ?", {}, $self->id);
 return $date;
}

But what I want is:

has name => (is => 'ro', isa => 'Str', lazy => 1, builder => '_build_stuff');
has birthdate => (is => 'ro', isa => 'Date', lazy => 1, builder => '_build_stuff');
has address => (is => 'ro', isa => 'Address', lazy => 1, builder => '_build_address');
sub _build_stuff {
 my $self = shift;
 my ($name, $date) = $dbh->selectrow_array("SELECT name, birthdate FROM people WHERE id = ?", {}, $self->id);
 $self->name($name);
 $self->birthdate($date);
}
sub _build_address { 
 #same as before 
}
  • 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-17T19:46:43+00:00Added an answer on May 17, 2026 at 7:46 pm

    What I do in this case, when I don’t want to have a separate object as in Ether’s answer, is have a lazily built attribute for the intermediate state. So, for example:

    has raw_row => (is => 'ro', init_arg => undef, lazy => 1, builder => '_build_raw_row');
    has birthdate => (is => 'ro', lazy => 1, builder => '_build_birthdate');
    
    sub _build_raw_row {
       $dbh->selectrow_hashref(...);
    }
    
    sub _build_birthdate {
        my $self = shift;
        return $self->raw_row->{birthdate};
    }
    

    Repeat the same pattern as birthdate for name, etc.

    Reading any of the individual attributes will try to get data from raw_row, whose lazy builder will only run the SQL once. Since your attributes are all readonly, you don’t have to worry about updating any object state if one of them changes.

    This pattern is useful for things like XML documents, too — the intermediate state you save can be e.g. a DOM, with individual attributes being lazily built from XPath expressions or what-have-you.

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

Sidebar

Related Questions

I have a navigation menu that has submenus slide down on hover. The menu
Is it possible to create an arbitrary shape on a web page so we
I have built a multi-touch application which is based on a Java EE backend
Is it possible to draw with your finger using the iPhone or iPad in
I have a fullscreen application running on Windows CE 6.0 which will only be
Do I have any simple way to have context menu items, that aren't highlighted
I am using the GL selection buffer to implement mouse picking. Unfortunately, OpenGL is
Is it possible to get image contents of an obstructed window without bringing it
I am using C#'s inbuilt drag and drop via Control.DoDragDrop(). I use an Image
I'm using an OpenFileDialog in my Windows Form. I would like this dialog to

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.