In FileReader.pm
In your script:package FileReader; use Moose; use Moose::Util::TypeConstraints; use IO::File; subtype 'FileHandleFromStr', as 'FileHandle'; coerce 'FileHandleFromStr', from 'Str', via { IO::File->new("< $_") }; has file => ( is => 'ro', isa => 'FileHandleFromStr', coerce => 1, ); sub read_next_line { my $self = shift; return $self->file->getline; } 1
Moose will take care of opening the file and attaching the filehandle to your object. You then call the method read_next_line on this object to ... read the next line.use Modern::Perl qw/2014/; use lib 'C:/Data/strawberry/script-chrome'; use FileReader; use Data::Dump qw /dump/; my $file = FileReader->new( file => 'C:/Data/strawberry/script-chrome/test.txt' + ); while ( my $line = $file->read_next_line ) { print $line; }
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
My blog: Imperial DeltronicsIn reply to Re: Moose reading big file line by line
by CountZero
in thread Moose reading big file line by line
by learning.moose
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |