in reply to Perl script to Parse a file for a string
#!/usr/bin/perl use strict; use warnings; use feature 'say'; my $return = find_string( file => "/path/to/file", positive_match => 1, string => "abc" ); say "did match? $return"; $return = find_string( file => "/path/to/file", negative_match => 0, string => "abc" ); say "did not match? $return"; sub find_string { my %params = @_; my $found = 0; # assumes that file is not tainted. # open my $fh, '<', $param{file} or die "Cannot opne $param{file}, +$!"; while (<DATA>) { $found++ if m/\Q$params{string}/ } return 1 if ( $params{positive_match} && $found > 0 ); return 0 if ( $params{positive_match} && $found == 0 ); return 1 if ( $params{negative_match} && $found == 0 ); return 0 if ( $params{negative_match} && $found > 0 ); } __DATA__ abc
Neil Watson
watson-wilson.ca
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl script to Parse a file for a string
by user786 (Sexton) on Jun 05, 2014 at 21:38 UTC |