OK; I think you're confused about what builders are supposed to do. Your builder will only be executed if you do NOT provide a value for the base_file attribute.

use v5.14; use Data::Dumper; package MyClass { use Moo; has base_file => ( is => 'rw', required => 1, init_arg => 'base', builder => sub { return 'bar' }, ); } # builder sub is *NOT* executed my $object1 = MyClass2->new(base => "foo"); print Dumper($object->base_file); # $VAR1 = 'foo' # builder sub *IS* executed my $object2 = MyClass->new(); print Dumper($object2->base_file); # $VAR1 = 'bar'

Further, you seem to be assuming that within the builder sub, $_[0] is some sort of file name. It's not; it's $self.

I think what you want is coercions...

use v5.14; use Data::Dumper; use IO::File; package MyClass { use Moo; has base_file => ( is => 'rw', required => 1, init_arg => 'base', coerce => sub { my $base_fh = IO::File->new( $_[0], '<' ) or die "$_[0]: $ +!"; $base_fh->binmode(":utf8"); return $base_fh; }, ); } my $object = MyClass->new(base => __FILE__); print Dumper($object->base_file);

In summary: use coercions to munge incoming values; use builders to provide a default value when there is no incoming value.

Here's another way you could have done it... provide a base attribute which is the filename as a string, and then build the base_file attribute from that:

use v5.14; use Data::Dumper; use IO::File; package MyClass { use Moo; has base => ( is => 'rw', required => 1, ); has base_file => ( is => 'rw', lazy => 1, builder => sub { my $base_fh = IO::File->new($_[0]->base, '<' ) or die($_[0 +]->base . ": $!"); $base_fh->binmode(":utf8"); return $base_fh; }, ); } my $object = MyClass->new(base => __FILE__); print Dumper($object->base_file);
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

In reply to Re: Confusion with Moo's builder attribute and a file handle by tobyink
in thread Confusion with Moo's builder attribute and a file handle by mgatto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.