in reply to increasing value every time sub routine called

the concept you are looking for is called a closure

  • Comment on Re: increasing value every time sub routine called

Replies are listed 'Best First'.
Re^2: increasing value every time sub routine called
by JavaFan (Canon) on Dec 09, 2011 at 14:07 UTC
    No, he doesn't. He isn't asking for multiple instances of counters. A single subroutine with an external variable (or an internal state variable) will do.
    sub next_number {no warnings;++$::N} say next_number; say next_number; say next_number; __END__ 1 2 3
      Well in perl land, a single (named) subroutine with a (lexical) external variable is still classed as a closure.

      Dave.

        Note that my solution does not use any lexical variables.