use strict; use warnings; # Initialise file names and open files, one # for reading, two for writing. # my $inputFile = q{spw579580.inp}; my $headerFile = q{spw579580.hdrs}; my $detailFile = q{spw579580.dets}; open my $inputFH, q{<}, $inputFile or die qq{open: $inputFile: $!\n}; open my $headerFH, q{>}, $headerFile or die qq{open: $headerFile: $!\n}; open my $detailFH, q{>}, $detailFile or die qq{open: $detailFile: $!\n}; # Set up start and end sentinels. # my $startSentinel = q{^L}; my $endSentinel = q{EOE}; # Compile regex to pull out records. Note the # \Q ... \E to quote regex metacharacters if your # record start and stop sentinels contain them. # my $rxExtractRecord = qr {(?xms) \Q$startSentinel\E\n (.*?) (?=\Q$endSentinel\E\n|\z) }; # Slurp whole file into string. # my $completeFile; { local $/; $completeFile = <$inputFH> } # Do a global match against compiled regex to # pull out records and put them in an array. They # will have the start and end record sentinels # removed by this process. # my @records = $completeFile =~ m{$rxExtractRecord}g; # Process each record in a loop. # foreach my $record (@records) { # We now have the record in a scalar # variable $record ready for processing. # # FURTHER PROCESSING GOES HERE ... } # Close open filehandles. # close $inputFH or die qq{close: $inputFile: $!\n}; close $headerFH or die qq{close: $headerFile: $!\n}; close $detailFH or die qq{close: $detailFile: $!\n};