I'm currently learning how to read through large groups of text files to scan for key words for some research work I'm doing. I came across an example that I don't fully understand, mostly because my Perl knowledge is a little piecemeal and it's been tough trying to find a reference to what's going on.

The following piece of code is part of a program that takes a long text file (2000+ lines), scans it for some key words, and then logs the file name and some other details if the key words are present. Most things I've written have looped through the lines one at a time, because that's currently all I know how to do.

I found an example that reads the entirety of the file into a variable at once, but I don't know what's going on syntactically. The following code reads in the each file:

{
# this step removes the default end of line character (\n)
# so the the entire file can be read in at once.
			local $/;
			open (SLURP, "$direct"."$file") or die "can't open $file: $!"; 
			$data = <SLURP>; 
		}

Then, I perform several simple regex matches using if statements.

What is the meaning of the local declaration in this case, and what does encapsulating the code segment do (if anything)? I get the comment that I am removing the end of line character, but I don't know how it's working.

[...]What is the meaning of the local declaration in this case, and what does encapsulating the code segment do (if anything)? I get the comment that I am removing the end of line character, but I don't know how it's working.

The example code modifies the Perl special variable that contains the value of the input record separator so that instead of reading one record at a time, the entire file is read into a scalar variable. You should restore the original value of $/ after the file is 'slurped' because $/ is a global variable that could affect any subroutines or imported module methods used by your script. One way to do this would be the following:

open my $fh, "<", "foo" or die $!;
my $save_input_record_separator = $/; #Save original value before changing it
undef $/; # enable slurp mode
my $content = <$fh>;
close $fh;
$/ = $save_input_record_separator; #Restore original value to this global variable

Another way is to enclose the code that needs the modified value of $/ within {brace or 'curly' brackets} and use the local operator to localise the modified value to the current block defined by the enclosing brace brackets. See http://perldoc.perl.org/perlvar.html where it says

You should be very careful when modifying the default values of most special variables described in this document. In most cases you want to localize these variables before changing them, since if you don't, the change may affect other modules which rely on the default values of the special variables that you have changed.

This article has been dead for over six months. Start a new discussion instead.