What is the correct way to create a BUILD method in MooseX :: Declare?

I'm having difficulty with a method BUILDin MooseX :: Declare . If I say:

#!/usr/bin/perl

use MooseX::Declare;

class Foo {
    has foo => (is => "rw", isa => "Str", default => "foo");

    method BUILD {
        print "I was called\n";
    }
}

Foo->new;

I get the following less useful error message:

Reference found where even-sized list expected at /Users/cowens/perl5/lib/perl5/MooseX/Method/Signatures/Meta/Method.pm line 335.
Validation failed for 'MooseX::Types::Structured::Tuple[MooseX::Types::Structured::Tuple[Object],MooseX::Types::Structured::Dict[]]' failed with value [ [ Foo=HASH(0x804b20) ], { HASH(0x8049e0) => undef } ], Internal Validation Error is: Validation failed for 'MooseX::Types::Structured::Dict[]' failed with value { HASH(0x8049e0) => undef } at /Users/cowens/perl5/lib/perl5/MooseX/Method/Signatures/Meta/Method.pm line 365
        MooseX::Method::Signatures::Meta::Method::validate('MooseX::Method::Signatures::Meta::Method=HASH(0xb8aab0)', 'ARRAY(0xb8ab30)') called at /Users/cowens/perl5/lib/perl5/MooseX/Method/Signatures/Meta/Method.pm line 139
        Foo::BUILD('Foo=HASH(0x804b20)', 'HASH(0x8049e0)') called at generated method (unknown origin) line 25
        Foo::new('Foo') called at test.pl line 13

But if I say:

#!/usr/bin/perl

use MooseX::Declare;

class Foo {
    has foo => (is => "rw", isa => "Str", default => "foo");

    sub BUILD {
        my $self = shift;
        print "I was called\n";
    }
}

Foo->new;

everything works just fine (but ugly and inappropriate with the rest of the code).

+5
source share
3 answers

BUILD accepts an argument, if you don't need it, just say:

method BUILD($) { ... }
+6
source

, BUILD . MooseX::Declare , , BUILD. ( .) , . , , , ; .

, , :

use MooseX::Declare;

class Foo {
    has foo => (is => "rw", isa => "Str", default => "foo");

    method BUILD(Item $href) {
        print "I was called\n";
    }
}

Foo->new;

, .

; , Moose hashref as-yet-unblessed .

+5

Perl sub, . & Foo:: BUILD, Devel::Declare, .

Moose specifically searches for subtext BUILDso that you can manipulate the constructor logic. My assumption (although I did not trace it to the end) is that the MooseX modules remain aloof from what Moose is trying to do. So the native is BUILDforever transmitted to Elk magic to determine the logic of the constructor.

On the other hand, the keyword is methodmore Devel :: Declare magic for creating methods in the metaclass structure.

-2
source

All Articles