Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Usage of regular expressions in input separator

by jdrago999 (Pilgrim)
on Jan 04, 2012 at 23:39 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-26 00:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found