in reply to Moose reading big file line by line

I'm not sure it is such a good idea to use Moose (or any other object system) for this task, but nothing tried, nothing gained!

In FileReader.pm

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
In your script:
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; }
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.

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 Deltronics

Replies are listed 'Best First'.
Re^2: Moose reading big file line by line
by Anonymous Monk on Apr 28, 2015 at 15:13 UTC
    Who said anything about Strawberry Perl?
      I just happen to use Strawberry Perl, but I fail to see the relevance of your comment.

      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 Deltronics