In my moo-based class, building this attribute works fine:

has +dbh => ( is => 'lazy', # must return only a code ref builder => sub { DBI->connect( "dbi:CSV:", undef, undef, { RaiseError => 1, PrintError => 1, f_ext => ".csv/r", # Better performance with XS csv_class => "Text::CSV_XS", # csv_null => 1, } ) or die "Cannot connect: $DBI::errstr"; } );
When dumped, as expected it returns: $VAR1 = bless( {}, 'DBI::db' );

But, this code returns just a string instead of a file handle, which fails when I try to performs ops on it:

has base_file => ( is => 'rw', required => 1, #allow external names to be different from class attribute init_arg => 'base', builder => sub { my $base_fh = IO::File->new( $_[0], '<' ) or die "$_[0]: $!"; $base_fh->binmode(":utf8"); return $base_fh; } );

This is what I get:

$VAR2 = '..\\t\\merge_into.csv';

which of course fails when wanting to pass it to Text::CSV_XS:

... Can't call method "getline" without a package or object reference

Is there some special case for handling file handles in Moo? I tried returning a ref to the filehandle instead, but no joy. It worked fine as a procedural script before I converted it into a class, so the strategy works.


In reply to 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.