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


in reply to Re^2: searching in large file
in thread searching in large file

Hello sabas

To answer your questions.

  1. yes
  2. In Perl, these metacharacters need to be escaped if they are to be matched literally \ | ( ) [ { ^ $ * + ? . (also called the dirty dozen) By using \Q ... \E, you escape any possible metacharacters in the variable being used for the regular expression - in this case $_
  3. He is compiling the regular expression $search_expression From Regexp Quote Like Operators the reason is Precompilation of the pattern into an internal representation at the moment of qr() avoids the need to recompile the pattern every time a match /$pat/ is attempted So, this avoids compiling the regular expression each time it is encountered in the while loop below next unless m/$search_expression/;
  4. Go to the top of the while loop and get the next line unless the regular expression matches this line. This skips the lines of code below if true.

Replies are listed 'Best First'.
Re^4: searching in large file
by NetWallah (Canon) on Jan 08, 2018 at 03:32 UTC
    Thank you, Cristoforo (++) for your excellent explanation.

    I wanted to add that for point (2), in addition to escaping the content($_) (with \Q and \E), the line appends the alternation meta-chaacter (|).

    This makes the regular expression search for ANY of the lines in the smallfile.
    Note also the "chop" statement which deletes the extra "|" appended.

                    We're living in a golden age. All you need is gold. -- D.W. Robertson.