Here you are:
use ChunkReader;
# create reader, threshold is optional (def. 2048)
my $reader = new ChunkReader (threshold=>1024);
# open file
$reader->open ("c:/test2.txt");
#iterate
while ( my $chunk = $reader->chunk )
{
print "$chunk\n****************\n";
}
Put this in a module "ChunkReader.pm"
package ChunkReader;
sub new
{
my $class = shift;
my %args = @_;
$args{threshold} = 2048
unless defined $args{threshold};
return bless {%args}, $class;
}
sub open
{
my $self = shift;
my $file = shift || $self->{file};
my $handle;
open $handle, "<", $file
or die "cannot open '$self->{file}\n'";
$self->{handle} = $handle;
}
sub chunk
{
my $self = shift;
my $handle = $self->{handle} || die "File not open!\n";
my $chunk = $self->{lastline};
# reset last line
$self->{lastline} = undef;
while ( my $line = <$handle> )
{
if ( ( length($chunk) + length($line) ) > $self->{threshold} )
+
{
# unless we already read a chunk
# (when a single line is bigger than the threshold)
unless ( $chunk )
{
#return the line
return $line;
}
else
{
# save the last line for further use
# and return the chunk
$self->{lastline} = $line;
return $chunk;
}
}
else
{
# append line and keep going
$chunk .= $line;
}
}
#end of file
return $chunk;
}
1;
Update:
Taken the advice from
TheDamian at
sub that sets $_, I updated the module so you can now do
use ChunkReader;
# create reader, threshold is optional (def. 2048)
my $reader = new ChunkReader ();
# open file
$reader->open ("c:/test2.txt");
while ( $reader->chunk )
{
print "$_****************\n";
}
package ChunkReader;
sub new
{
my $class = shift;
my %args = @_;
$args{threshold} = 2048
unless defined $args{threshold};
return bless {%args}, $class;
}
sub open
{
my $self = shift;
my $file = shift || $self->{file};
my $handle;
open $handle, "<", $file
or die "cannot open '$self->{file}\n'";
$self->{handle} = $handle;
}
sub chunk
{
my $self = shift;
my $handle = $self->{handle} || die "File not open!\n";
my $chunk = $self->{lastline};
# reset last line
$self->{lastline} = undef;
while ( my $line = <$handle> )
{
if ( ( length($chunk) + length($line) ) > $self->{threshold} )
+
{
# unless we already read a chunk
# (when a single line is bigger than the threshold)
unless ( $chunk )
{
#return the line
return bless \$line, ChunkReader::Chunk;
}
else
{
# save the last line for further use
# and return the chunk
$self->{lastline} = $line;
return bless \$chunk, ChunkReader::Chunk;
}
}
else
{
# append line and keep going
$chunk .= $line;
}
}
#end of file
return bless \$chunk, ChunkReader::Chunk;
}
package ChunkReader::Chunk;
use overload
(
q{bool} => sub { return ${$_[0]} },
q{""} => sub { return ${$_[0]} },
q{0+} => sub { 0 + ${$_[0]} },
fallback => 1,
);
sub DESTROY
{
$_ = ${$_[0]};
}
1;
__END__
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.