in reply to Perl script to Parse a file for a string
Your indentation doesn't help see the flow of the code. Reformatted in a more conventional fashion:
sub find_string { my ($file, $string) = @_; open my $fh, '<', $file; while (<$fh>) { return 1 if /\Q$string/; } die "False :Test result is FAIL"; find_string('search.txt', 'name: abc '); }
it is clear that the die prevents the recursive call to find_string.
However, if the file is not huge (say, bigger than a few hundred megabytes), just suck the whole thing into a string and run the regex against it directly:
sub find_string { my ($file, $string) = @_; open my $fh, '<', $file or die "Can't open '$file': $!\n"; my $text = do {local $/; <$fh>}; return $text =~ /\Q$string/; }
|
|---|