Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Very nice sugar, thank you. Posting to say, I got extra-lazy and decided I wanted it even easier.

Holli's code has two methods, one to open the file and the rest to return it in chunks.

$reader->open ("/path/to/file.txt"); #iterate while ( my $chunk = $reader->chunk ) { print "$chunk\n****************\n"; }
Whereas I'm so lazy I wanted to do just this:
while ( my $chunk = $reader->chunk("/path/to/file.txt") ) { print "$chunk\n****************\n"; }

So I moved some code around and got this, which works just fine:

package ChunkReader; sub new { my $class = shift; my %args = @_; $args{threshold} = 2048 unless defined $args{threshold}; return bless {%args}, $class; } sub chunk { my $self = shift; unless ( $self->{handle} ) { my $file = shift || $self->{file}; print "Filename is $file\n"; my $handle; open($handle, "<", $file) or die "cannot open '$self->{file}\n'"; $self->{handle} = $handle; } my $handle = $self->{handle} || die "File not open!\n"; my $chunk = $self->{lastline}; # reset last line $self->{lastline} = ''; while ( $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;


($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
=~y~b-v~a-z~s; print

In reply to Re^4: Handling A File In Human-Compatible Chunks by Cody Pendant
in thread Handling A File In Human-Compatible Chunks by Cody Pendant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2023-06-03 01:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you go to conferences?






    Results (6 votes). Check out past polls.

    Notices?