G'day JediWombat,
Welcome to the Monastery.
"This uses the $/ input field separator, and then uses while (<>) to read a block at a time. I'd like to do this in pure perl, but I can't find a way."
Firstly, here's a simple example of how you might do this.
#!/usr/bin/env perl -l use strict; use warnings; { local $/ = ''; while (<DATA>) { chomp; print '--- One Block ---'; print; } } __DATA__ Block1 Line1 Block1 Line2 Block1 Line3 Block2 Line1 Block2 Line2 Block2 Line3 Block3 Line1 Block3 Line2 Block3 Line3 Block4 Line1 Block4 Line2 Block4 Line3
Notes:
The output looks like this:
--- One Block --- Block1 Line1 Block1 Line2 Block1 Line3 --- One Block --- Block2 Line1 Block2 Line2 Block2 Line3 --- One Block --- Block3 Line1 Block3 Line2 Block3 Line3 --- One Block --- Block4 Line1 Block4 Line2 Block4 Line3
I thought ++roboticus had generally covered issues relating to '$/' and IO::Uncompress::Bunzip2; however, your reply seems to suggest you were looking for something else.
I'm not entirely sure what you're looking for. Note in IO::Uncompress::Bunzip2's Constructor section:
... the object, $z, returned from IO::Uncompress::Bunzip2 can be used exactly like an IO::File filehandle. This means that all normal input file operations can be carried out with $z. For example, to read a line from a compressed file/buffer you can use either of these forms
$line = $z->getline(); $line = <$z>;
Try using '<$z>', in a way similar to my example with '<DATA>', and see if that does what you want. Something like this (untested):
my $z = IO::Uncompress::Bunzip2::->new($filename); { local $/ = ''; while (<$z>) { ... } }
Note that the constructor code I've used differs from that shown in the IO::Uncompress::Bunzip2 documentation. This is on purpose and I recommend you use this instead. The IO::Uncompress::Bunzip2 documentation uses "Indirect Object Syntax: if you follow that link, you'll see in bold text
"... this syntax is discouraged ..."
along with a discussion of why that syntax should be avoided.
— Ken
In reply to Re: Searching large files a block at a time
by kcott
in thread Searching large files a block at a time
by JediWombat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |