As BrowserUk and moritz said, it make no sense to read the whole file into a single variable. Much better to read the file in line by line, discarding the lines you don't need, and storing each line you are interested in in a separate variable. You can keep this list of scalar strings in an array or another data structure.
If each line requires lots of work to process, then your overall problem sounds ideal for Parallel::ForkManager I would try something like:
use strict; use English; use Parallel::ForkManager; # Experiment with this value. I suggest you initially try setting # it to twice the number of CPU threads you have. my $MAX_PROCESSES = 8; $pm = new Parallel::ForkManager($MAX_PROCESSES); open my $src_FH, '<', 'Huge_source_file.txt' or die "Error opening sou +rce file $!"; my $line_num = 0; LINE: while( my $line = <$src_FH> ) { next LINE unless 1 == ($INPUT_LINE_NUMBER % 4); my $worker_pid = $pm->start; if( $worker_pid ) { # In parent next LINE; } else { # Call a subroutine to process the line process_line($worker_pid); $pm->finish; # Terminates the child process } }
Parallel::ForkManager will maintain a pool of worker threads, and pass processing jobs to each, so you don't need to worry about creating a fork bomb by mistake. If you need to get results back from the worker threads, then there are docs on CPAN explaining how.
In reply to Re: buffering from a large file
by chrestomanci
in thread buffering from a large file
by cedance
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |