in reply to Matching problem

if you dont want to loop through file line by line then you could also do this:
open (FILE, "<file.html") || die "couldn't open file: $!\n"; my @data = <FILE>; my $file = join /\n/, @data; if ($file =~ m{$searchword}g) { #do something }

Replies are listed 'Best First'.
Re^2: Matching problem
by graff (Chancellor) on Jul 09, 2004 at 01:55 UTC
    Rather than making two copies of the full file content in memory (and wasting cycles on a useless join), it would be better either to slurp the whole file into a scalar, like this:
    { local $/ = undef; open(FILE,$filename) or die "open failed on $filename: $!"; $wholetext = <FILE>; close FILE; } if ( $wholetext =~ /\b$searchword\b/ ) { # do something }
    Or to read the file into an array and use grep on the array, like this:
    open( FILE, $filename ) or die "yadda yadda"; @data = <FILE>; if ( grep /\b$searchword\b/, @data ) { # do something; }