http://www.perlmonks.org?node_id=1196493

JediWombat has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I'm fairly new to Perl, but I'm in a job where a lot of existing Perl code is in use, so I'm working off what I have available and might be getting in over my head. I'm hoping you can help. I have a large LDIF file in bzip format that I want to search, and for the block (as defined by blank lines before and after it) that matches my pattern, print the block. Looking at a shell script we have here, which pipes into perl:
/usr/bin/bzcat $ldif | perl -e "$/ = \"\n\n\"; while (<>) { if (/uid=$match/) { print $_ ; last; } ; }"
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. I'm using the IO::Uncompress::Bunzip2 module, which gives me an IO:File object, but the only way I can seem to interact with this in a useful way is with getline() or getlines() - neither of which let me iterate through the file a block at a time, like the shell script does. Like I said, I am pretty new to Perl, so I'm sure there's a better way to do this. Can someone offer some assistance please? Here's the code I've got, which works, but is very slow:
my $z = new IO::Uncompress::Bunzip2 $file; $mbnum = $ARGV[0]; while ($line = $z->getline()) { if ($line =~ /^dn: uid=$mbnum,/) { $found = "true"; print $line; for (my $i=0; $i<100; $i++) { $matchLine = $z->getline(); print "$matchLine"; if ($matchLine =~ /^$/) { last; } } } if ($found eq "true") { last; } }