mgatto has asked for the wisdom of the Perl Monks concerning the following question:
In my moo-based class, building this attribute works fine:
When dumped, as expected it returns: $VAR1 = bless( {}, 'DBI::db' );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"; } );
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 referenceIs 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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Confusion with Moo's builder attribute and a file handle
by tobyink (Canon) on Jul 23, 2013 at 08:40 UTC | |
by mgatto (Novice) on Jul 23, 2013 at 14:45 UTC | |
|
Re: Confusion with Moo's builder attribute and a file handle (code)
by Anonymous Monk on Jul 23, 2013 at 00:44 UTC |