Because of the way Module evaluates the list of symbols it is not possible to refer to earlier scoped symbols. I usually specify what names should be lexically scoped and set them in the body e.g.
f2[m_?MatrixQ]:=Module[{d, n, e},
d = Length[m];
n = 1;
e = IdentityMatrix[d];
...
]
Block permits references to earlier scoped symbols. If a symbol is never reassigned a value then a combination of With and Module can be used e.g.
Your last method should be changed into the following for using with the example I posted here:
In[27]:= (*Block permits references to earlier scoped symbols. If a symbol is never reassigned a value then a combination of With and Module can be used e.g.
*)
f3//ClearAll;
f3[m_?MatrixQ] :=
With[{d = Length[m]},
Module[{n=1,e = IdentityMatrix[d]},
NestWhile[(n++;Dot[#,m])&,m,#!=e&];n
]]
f3/@A3
Out[29]= {1, 3, 3}