in reply to Running a subroutine a certain number of times

If you were using strict, you could see that $i (in the main program) and $i (in the subroutine) are the same variables, since they're both global. Using strict would prevent this..

Regards,
-octo

Replies are listed 'Best First'.
Please, please don't treat use strict as a magic wand that solves all problems.
by Abigail-II (Bishop) on Aug 05, 2002 at 14:30 UTC
    I was waiting for someone to say that.

    No, use strict would not magically solve the problem. By putting use strict on top of the code, and my $i above the first loop, you would still have had the same problem: the loop variable is still shared.

    You solve the problem by putting my $i in the subroutine, or in the inner block. But you don't need use strict to do so.

    Abigail

      True, but I find that getting people in the habit of using strict also gets them in the habit of scoping all their variables properly, so the mistake becomes far less likely to happen.
      But I agree, use strict is not a substitute for use your_brain.


      -pete
      "Pain heals. Chicks dig scars. Glory lasts forever."

      I did not say that 'use strict' magically solves all your problems, but that this mistake would not have been as easy to happen.

      Regards,
      -octo