http://qs321.pair.com?node_id=1188177


in reply to Perl reading in a file and getting a string in between two strings on different lines

This appears to meet your spec:
#!/usr/bin/perl use strict; use warnings; use 5.10.0; use Data::Dumper; my $beginString = "SEARCH"; my $endString = "TEST"; my $fileContent; { local $/; $fileContent = <DATA>; } if($fileContent =~ /\b$beginString\b(.*?)\b$endString\b/s){ my $result = $1; print $result; } #print Dumper($fileContent); __DATA__ #Ignoring this SEARCH => #Beginning of data I need ....... ........ #all the data I need ....... ..... #End of Data I Need TEST => # And ignoring this
Given that it's largely taken from your posted code, I wonder if you accidentally fixed your issue in preparing the post. If this does not output what you need, please describe how.

Sidenote 1:

my $fileContent; open(my $fileHandler, $inputFile) or die "Could not open file '$inputF +ile' $!"; { local $/; $fileContent = <$fileHandler>; } close($fileHandler);
You can rewrite this as
my $fileContent = do { open(my $fileHandler, $inputFile) or die "Could not open file '$in +putFile' $!"; local $/; <$fileHandler>; };
and avoid scoping challenges on that file handle.

Sidenote 2: When using start/finish tags, assuming you want literals, you should quotemeta:

if($fileContent =~ /\b\Q$beginString\E\b(.*?)\b\Q$endString\E\b/s){

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: Perl reading in a file and getting a string in between two strings on different lines
by victorz22 (Sexton) on Apr 17, 2017 at 22:33 UTC

    Thanks your code helped out!