Back reference of variable in Module?

See the following example:

In[904]:= (*
The Abelian group of order 3 has these elements:
*)
A3={IdentityMatrix[2],RotationMatrix[2 \[Pi]/3],RotationMatrix[4 \[Pi]/3]};

f1//ClearAll;
f1[m_?MatrixQ]:=Module[{n=1,e=IdentityMatrix[Length[m]]},
NestWhile[(n++;Dot[#,m])&,m,#!=e&];n]

f2//ClearAll;
f2[m_?MatrixQ]:=Module[{
d=Length[m],
n=1,
e=IdentityMatrix[d]
},
NestWhile[(n++;Dot[#,m])&,m,#!=e&];n]

f1/@A3
f2/@A3

Out[909]= {1, 3, 3}

Out[910]= {1, 1, 1}

So, how to do the back reference of variable in Module?

Regards,
Zhao

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.

f2[m_?MatrixQ] := 
 With[{d = Length[m], n = 1}, 
  Module[{e = IdentityMatrix[d]}, ...]]

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}