Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^3: Usage of regular expressions in input separator

by jdrago999 (Pilgrim)
on Jan 04, 2012 at 23:39 UTC ( #946307=note: print w/replies, xml ) Need Help??

in reply to Re^2: Usage of regular expressions in input separator
in thread Usage of regular expressions in input separator

Thanks for giving my "wouldn't it be cool if..." the full treatment.

In the meantime, I suppose we'll have to:

open my $ifh, '<', $filename or die "Cannot open '$filename' for reading: $!"; local $/; foreach my $chunk ( split /Separator\s+\d+/, scalar(<$ifh>) ) { # yay chunk! }

Unfortunately this will not do well for very large files. We'd have to check against the regexp as each byte is read into memory.

# I might be way off-base here: no warnings 'uninitialized'; my $pattern = qr{Separator\s\d+}; my $callback = sub { warn "Chunk: @_" }; binmode($ifh); my $offset = 0; my $buffer = ''; while( sysread($ifh, my $byte, 1, $offset++) ) { $buffer .= $byte; if( $buffer =~ $pattern ) { $callback->( $buffer ); $buffer = ''; } }

Even that won't work correctly, and it would be really, really slow.

I only wrote it here for the sake of discussion.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946307]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2020-05-29 08:09 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (167 votes). Check out past polls.