in reply to Variable in a foreach loop

That isn't the code you are running because it won't even compile.

$ perl 1167769.pl Global symbol "$max_cols" requires explicit package name at 1167769.pl + line 8. Global symbol "$max_rows" requires explicit package name at 1167769.pl + line 10. Execution of 1167769.pl aborted due to compilation errors.