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


in reply to Splitting a file based on matched conditions

Below is some "starter code" for you showing another method of doing this. Rather setting flags in the main body of the code, calling a subroutine to handle processing the record can be a good way. If you are in the subroutine, then that means that you are inside the record - no need to have a specific flag for that status..

100K lines isn't big enough to worry much about seeking around. I'd just save the data in a buffer and then decide what to do with it when you see the End line.

You can also look at: Flipin good, or a total flop?

I use a variety of methods for similar tasks with some attention to who I'm writing the code for and the level of Perl expertise I expect them to have. Most of the time the number of lines of code is meaningless as long as it is clear and well structured.

#/usr/bin/perl use strict; use warnings; #### Not complete code #### #### This is just a starting framework ### while (my $line = <DATA>) { handle_section ($line) if $line =~ /^Start:/; } sub handle_section { my $start_line = shift; my @buf; my ($parm) = $start_line =~ /Start:(.*)/; push @buf, $start_line; print "Staring Parm is: $parm\n"; my $line; while (defined ($line = <DATA>) and $line !~ /End/) { push @buf, $line; } # do something based upon End line (still in $line) # If not an End line, then malformed record # end of data was reached without an End line } __DATA__ Start:/abc/def ..... End 1.2 Start:/xyz/uvw ..... End 2.8