Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Elaborating on my question Safely reading line by line, I have tried to put together a IO::Handle::getline variant with a size constraint on its return value.

It could be used like this:

my $fh = IO::File::Narrow->new('/path/to/file', 'r'); local $IO::File::Narrow::max_input_record_length = 80; local $/ = "\n"; while (defined(my $line = $fh->getline)) { # do something with line }

It turns out supporting various flavours of $/ is not much fun. I am not too fond of dynamically scoped variables either, but IO::* modules have them anyway.

package IO::File::Narrow; use 5.006001; use strict; use base qw(IO::File); use Carp qw(croak); our $VERSION = '0.01'; our $max_input_record_length = 1024; sub getline { @_ == 1 or croak 'usage: $io->getline()'; my $this = shift; my $line = q{}; return undef if $this->eof; my $irs = ref($this)->input_record_separator; if (!defined $irs) { while (defined(my $ch = $this->getc)) { $line .= $ch; croak 'input record too long' if $max_input_record_length < length $line; } return $line; } if (ref $irs) { my $frl = int(${$irs}) || 0; while (defined(my $ch = $this->getc)) { $line .= $ch; croak 'input record too long' if $max_input_record_length < length $line; return $line if $frl == length $line; } return $line; } if (1 == length $irs) { while (defined(my $ch = $this->getc)) { $line .= $ch; croak 'input record too long' if $max_input_record_length < length $line; return $line if $ch eq $irs; } return $line; } if (q{} ne $irs) { while (defined(my $ch = $this->getc)) { $line .= $ch; croak 'input record too long' if $max_input_record_length < length $line; return $line if substr($line, -length $irs) eq $irs; } return $line; } else { my $pch = q{}; while (defined(my $ch = $this->getc)) { $line .= $ch; croak 'input record too long' if $max_input_record_length < length $line; if ("\n" eq $ch && "\n" eq $pch) { while (defined($ch = $this->getc)) { if ("\n" ne $ch) { $this->ungetc(ord $ch); return $line; } } return $line; } $pch = $ch; } return $line; } } sub getlines { @_ == 1 or croak 'usage: $io->getlines()'; wantarray or croak 'Can\'t call $io->getlines in a scalar context, use $io- +>getline'; my $this = shift; my @buffer = (); while (defined(my $line = $this->getline)) { push @buffer, $line; } return @buffer; } 1; __END__ # TODO: pod documentation

In reply to line-by-line input with size limit by martin

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?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 17:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found