Home > front end >  Moose Attribute Builder Calling own Class function
Moose Attribute Builder Calling own Class function

Time:11-12

I am implementing a class in Perl Moose on a bigger project. The issue that I am having is the following:

Lets assume that I have a class method called GetValue() returning an HashRef and an attribute called Value, which is an HashRef.

This is what I am trying to do:

sub GetValue
{
 #do something
 return \%something;
}

has Value => (
    is=>'ro',
    isa => 'HashRef',
    reader => "Value",
    builder => '_Value_builder',
);

sub _Value_builder
{
  my $self = shift;
  return $self->GetValue();
}

This does not seem to work, Since I am on really big project the error coming out is just a very long stack trace that to my opinion does not hold too much information.

In the real case the function GetValue is a TCP call too a server, socket is part of the class attributes. For what I understand the issue seems to be that the call to GetValue takes place before the builder created the socket, but I am not sure.

Can this syntax work at all? In case the TCP communication is the issue, is there a workaround?

CodePudding user response:

As discussed in the comments already the issue is solved declaring the attribute as lazy:

sub GetValue
{
 #do something
 return \%something;
}

has Value => (
    is=>'ro',
    isa => 'HashRef',
    reader => "Value",
    lazy => 1,
    builder => '_Value_builder',
);

sub _Value_builder
{
  my $self = shift;
  return $self->GetValue();
}

  • Related