|Think about Loose Coupling
You are misunderstanding the use of grep.
In your example, grep will always return $l unless $m happens to be 0 or an empty string.
Instead of grep, you probably want a regular expression.
You probably also want to use chomp in the outer loop to remove the line ending from $m.
You are also writing the file multiple times with different lines commented each time. You might want to generate a list of the expressions to match at the beginning and then loop over the file once.