>From: Brian F. Yulga [mailto:byu...@langly.dyndns.org]
>On Wed, 2 Mar 2011, Matt wrote: > > The easiest way in my opinion is to use the 'grep' function like this: > > > > my $searchstring="whatever"; > > open CFG, '<', $_file || die("could not open file: $_file!"); my > > @data=<CFG>; close CFG; if ( grep /$searchstring/i, @data ) { > > print "$searchstring found\n"; > > } > > > > This sorta worked. Needed a minor change. > > > unless ( grep /$searchstring/i, @data ) { print "$searchstring not > > found\n"; > > Thanks. > > >My apologies if I'm beating a dead horse here, but I'm new to Perl and thought >of a slightly different approach: > > >my $searchrx = qr/whatever/; # or q/whatever/ if you don't need regexp This is probably personal preference, but I prefer to provide a more meaningful name rather than using @ARGV. Plus you have lost the filename once you 'shifted' @ARGV in the open statement. May want to use the file name in the open error statement or later. Also, I would not use the '/' as your quote delimiter - that's too recognizable as being used as the pattern match delimiter. Use actual double quotes (") unless there are double quotes in the string - also less typing. Instead of '/' may want to use '{' and '}' if using qq (see perldoc perlop, Quote and Quote-like Operators). my $fileName = shift @ARGV or die "You did not specify a filename\n"; >@ARGV or die qq/you didn't specify a filename\n/; Use lexical variable for filehandle. Makes things easier - such as passing to a function. open my $FH,'<', $fileName or die "Error opening $fileName: $!\n"; >open FH, q/</, shift @ARGV or die qq/file open error: $!/; The join is unnecessary, set the input_record_separator ($/) to undef or use a local copy of $/ (see perldoc perlvar). If this is undefined, the entire file will be read into a variable. >$_ = join q//, <FH>; >close FH; >if ( ! m/$searchrx/s ) { > print qq/pattern not found\n/; > # do something >} > > >I'm not sure which is more "Perlish", but I hear "TMTOWTDI" is supposed to >be valued, too. I welcome any criticism. > >Brian HTH, Ken -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/