I'm trying to understand some Perl code that is used to read a text file block-wise.
The text file MYFILE
looks like this:
First block
First Line: Something in here
Second Line: More here
Third Line: etc.
Second block
First Line: Something in here
Second Line: More here
Third Line: etc.
The code is used to extract the lines of a block where a regular expression is found (and it works fine, I just want to understand it).
This is the part of the code that I don't understand:
local $/ = q||;
while (<MYFILE>) {
do something;
}
Could someone explain to me what the line local $/ = q||;
is doing?
$/
is the input record separator. "This influences Perl's idea of what a "line" is". Setting it to empty string, i.e., ''
causes the blank line to split the records. q||
notation quotes the stuff within pipes so q||
is the same as ''
. You can use a variety of delimiters with the q
prefix: q(), q//
are also the same.
local $/ = q||;
This will treat empty line as "record separator".
local $/ = q||;
while(<$fh>) {
# each loop, $_ will be a different record
# the first will be "First block\nFirst Line: Something in here\nSecond Line: More here\nThird Line: etc.\n\n"
# etc.
}
local $/ = q||;
is same as local $/ = '';
Check Quote and Quote-like Operators.
local
will temporarily using dynamic scope set global variable $/
(input record separator) to empty string, so input records are terminated by one or more empty lines.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With