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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:26:33+00:00 2026-05-14T07:26:33+00:00

I have to read a file in the BUILD method and I want to

  • 0

I have to read a file in the BUILD method and I want to use the load method of the MooseX::Storage package.
But this load method create a new object and so when I instatiate the object this isn’t the object read from file. In the code below I create an object $m1 with state 2 to write the file, I create $m2 with no parameter to read the file but $m2 doesn’t contain the right value.
The package:

package mia;
use Moose;
use MooseX::Storage;

with Storage(format => 'JSON', io => 'File');

has 'nome'  => ( is     => 'rw', isa    => 'Str', default =>'',);
has 'stato' => ( is     => 'rw', isa    => 'Int', default =>1,);

sub BUILD { 
    my $self=shift;
    if ($self->stato==1){
        $self=mia->load("mia.dat");
    }
    if ($self->stato==2){
        $self->stato(0);
        $self->nome("prova");
        $self->store("mia.dat");    
    }
}

sub stampa(){
    my $self=shift;
    print $self->nome." ".$self->stato;
}

the main program

use mia;
my $m;

$m1=mia->new(stato=>2); 
$m2=mia->new();
print "\nm1 \n";
$m1->stampa();
print "\nm2 \n";
$m2->stampa();
  • 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-14T07:26:33+00:00Added an answer on May 14, 2026 at 7:26 am

    Your code seems to be acting as if BUILD is a constructor, which it isn’t — it’s more like a post-construction hook where you can perform other things like read values from a DB. You should instead either:

    • store the result of mia->load in an attribute, and optionally use delegated methods to access it, or
    • use the result of mia->load as the object, rather than constructing a separate one.

    Here is an example of the first case, separating the MooseX::Storage object from the object that controls it:

    package miaController;
    use Moose;
    use mia;
    
    has 'nome'  => ( is     => 'rw', isa    => 'Str', default =>'',);
    has 'stato' => ( is     => 'rw', isa    => 'Int', default =>1,);
    has 'mia'   => ( is     => 'rw', isa    => 'mia', lazy => 1);
    
    sub BUILD
    {
        my $self = shift;
        if ($self->stato == 1)
        {
            $self->mia(mia->load("mia.dat"));
        }
        elsif ($self->stato == 2)
        {
            $self->stato(0);
            $self->nome("prova");
            $self->mia->store("mia.dat");
        }
    }
    sub stampa
    {
        my $self = shift;
        print $self->nome." ".$self->stato;
    }
    
    package mia;
    use Moose;
    use MooseX::Storage;
    with Storage(format => 'JSON', io => 'File');
    
    package main:
    use miaController;
    
    my $m1=miaController->new(stato=>2);
    my $m2=miaController->new();
    print "\nm1 \n";
    $m1->stampa();
    print "\nm2 \n";
    $m2->stampa();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an excel file via C#. I have read a few
I have read a file into a String. The file contains various names, one
My current situation is: I have to read a file and put the contents
I have to read a text file and based on some logic, make some
I have read that CURL is way too fast than File Get Contents and
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
I am having following issue with reading binary file in C. I have read
By using filestreaming in c++, I have read a string in the binary file
In support of some legacy code, I have to read a text file and
I want to read the text from the localizable.strings file. I am collecting 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.