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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:18:38+00:00 2026-06-10T07:18:38+00:00

In my object constructor i had statement to initialize two attributes same time: ($self->{token},

  • 0

In my object constructor i had statement to initialize two attributes same time:

($self->{token}, $self->{token_start}) = $self->_get_authorized_token();

So i got token and it’s starting time together in one statement.

Now i try to port my module to use Moo(se) and here i don’t know how should i set those two bound attributes at same time?. Some pseudo code would be like that:

has qw/token token_start/ => (
    is  => 'rw',
    default => shift->_get_authorized_token(); 
);

But how to declare 2 bound attributes in Moo(se)ish way?


EDIT. I show the code of the method _get_authorized_token, maybe it will brings some ideas:

sub _get_authorized_token {
    my $self = shift;
    my $postData = { 'apikey' => $self->{key} };
    my $url = $self->{base_url} . '/seller';
    my $xml = $self->_post(url => $url,
                            postdata => $postData,
                        );
    my $ref = XMLin($xml, SuppressEmpty => '' );
    my $time = $ref->{Notification_Datetime};
    my $token = $ref->{Notification_Data}{body}{token};
    return ($token, $time); 
}
  • 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-10T07:18:38+00:00Added an answer on June 10, 2026 at 7:18 am

    Once you end up with two attributes that are basically linked to the point where you always set them simultaneously … the answer is usually to create a value object with two attributes for the purpose and then delegate the relevant methods to it. So, something like –

    package MyApp::TokenInfo;
    
    use Moo;
    
    has token => (is => 'ro', required => 1);
    has token_start => (is => 'ro', required => 1);
    
    ...
    
    package MyApp::ThingWithAToken;
    
    use Module::Runtime qw(use_module);
    use Moo;
    
    ...
    
    has token_info => (is => 'lazy', handles => [ qw(token token_start) ]);
    
    sub _build_token_info {
      my ($self) = @_;
      my ($token, $token_start) = $self->_get_authorized_token;
    
      # this is equivalent to:
      #
      #   require MyApp::TokenInfo;
      #   return MyApp::TokenInfo->new(...);
      #
      # but more concise
    
      return use_module('MyApp::TokenInfo')->new(
        token => $token,
        token_start => $token_start
      );
    }
    
    ...
    
    my $thing = MyApp::ThingWithAToken->new(...);
    
    $thing->token; # calls $thing->token_info->token;
    $thing->token_start; # calls $thing->token_info->token_start
    

    so the presence of the value object isn’t required knowlede from externally but internally you’ve still got the two attributes tied together in a way that lets your implementation handle them as a single “thing”.

    — mst

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

Sidebar

Related Questions

Following on from another question I had here I've got a MainConfig object and
I had some code that constructed an object: function gridObjConst(id, itemName, itemPrice, itemListPrice, width,
In Perl, if I want to use named parameters in an object constructor, my
I heard that a private constructor prevents object creation from the outside world. When
When you instantiate a new object by calling constructor, i.e. Foo bar = new
Can I do this without reference to the object in the constructor? In other
In the constructor of an object I have the following code: ConfigReader::ConfigReader(){ TiXmlDocument doc(CONFIGURATION_FILE_LOCATION);
Doesn't object initialization outside of a constructor break encapsulation ? Given: class MyClass {
I have an object type with no-args constructor, but when I specify it as
I have a Uri object being passed to a constructor of my class. I

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.