You seem confused about what your program does, so I thought I'd just tell you.
$inFile = "animals.txt";
open (IN, $inFile) or die "Can't find file: $inFile";
@animallist = (<IN>);
# here you define a file name, open a file, and read all of the lines
# in the file into the array @animallist
foreach $line (@animallist) {
# here you iterate over all the lines, putting each line into $line
if ($line =~ $search) {
print "$line <br> <br>";
}
# here you perform the regex match: $line =~ /$search/ and if it
# succeeds, print $line
}
# here you end the loop
if ($line ne $search) {
print "$search isn't in the animal list";
}
# here you take the now uninitialized variable $line and try to match
# it against the as of yet undefined variable $search
# If both of these variables are undefined, and you are not using warnings
# it will simply return false (because "" ne "" is false)
# without warning about undefined variables in ne
You should be aware that even if your entire line was, for example, cat
, you still could not compare it using ne
to the string cat
, because when read from a file, it has a trailing newline, so it is really cat\n
. Unless you chomp
it.
It seems redundant to tell you, but of course you cannot check if the file does not contain $search
after you finished reading the file. You have to do that while reading the file.