Feel free to save this source code into a file of your own to try out
and/or experiment with.
#!/usr/bin/perl -w
# Here's a small program that can be used to search for a string
# in a file or file(s). You just invoke the program from the
# command line with the file(s) you want to search like this:
#
# ./searchfor.pl file
#
# The program will then prompt you for a string for which to search,
# and then will search all lines of the file(s) for that string,
# printing lines that match (i.e. include the string as a substring).
# Note that the search is done ignoring the case of the letters.
#
# (This program is a very, very basic "grep"-like search program, but
# not very sophisticated at all.)
use strict;
my ($str, );
print "\nEnter search string: ";
chomp ( $str = <STDIN> );
print "\n";
# search all the lines of the file(s)
while (<>)
{
if ( /${str}/i ) # Look for a match, ignoring case.
# This is how you use a variable name
# in a regular expression. The "i" option
# tells the regular expression engine to
# ignore case.
{ print; }
}
print "\n";