in reply to Searching large files a block at a time

Hello JediWombat,

Perl may call bzcat directly via open, similarly to calling bzcat from shell.

See tip by kcott for blank input field separator. Thanks kcott for the tip.
See tip by Anonymous Monk for reading from bzcat directly.

#!/usr/bin/perl use strict; use warnings; my $ldif = "file.ldif.bz2"; my $match = "g2ucab"; $/ = ""; open my $fh, "-|", "/usr/bin/bzcat $ldif" or die "open error ($ldif): $!"; while ( <$fh> ) { if ( /uid=$match/m ) { print $_; last; } } close $fh;

Parallel processing is another possibility when involving extra work inside the block, which isn't the case here. Thus, the next demonstration will not run any faster. Workers read 8 blocks at a time, configurable via the chunk_size option. Calling $mce->last causes all workers to leave the parallel block, similarly to calling last inside a while loop.

The reason that this small use-case doesn't run any faster is due to workers waiting on bzcat.

#!/usr/bin/perl use strict; use warnings; use MCE::Loop; my $ldif = "file.ldif.bz2"; my $match = "g2ucab"; $/ = ""; open my $fh, "-|", "/usr/bin/bzcat $ldif" or die "open error ($ldif): $!"; MCE::Loop->init( max_workers => 3, chunk_size => 8, use_slurpio => 1, ); mce_loop { my ( $mce, $slurp_ref, $chunk_id ) = @_; open my $local_fh, "<", $slurp_ref; while ( <$local_fh> ) { if ( /uid=$match/m ) { print $_; $mce->last; } } close $local_fh; } $fh; MCE::Loop->finish; close $fh;

Regards, Mario