http://www.perlmonks.org?node_id=833674


in reply to Re^2: Perl Project: Text Search
in thread Perl Project: Text Search

Your two elsif blocks don't belong in the loop. If they're in the loop, they execute for every line of the file, which is not what you want.

You only know for sure that a username wasn't in the file after you've worked through every single line of the file. In fact, since you immediately exit the program after finding and printing the information for a user, you know that if you make it through the entire while(<FILE>) loop, the username wasn't found. Print the error message at that point.

Similarly, the check that exactly one argument was provided needs to be done before you enter the while loop, not within it. Why even open the passwd file if the program wasn't run correctly, right?

Update: the indentation of your code is strange - you do realize that the if/elsif/elsif is within the while loop - hence is being executed once for every line in the input file? Nesting in Perl is controlled with curly braces ({ and }), not by indentation like in some other languages.