in reply to my variables in a subroutine

Further to ikegami's post, in case you are not using 5.10 yet, the following 'hack' works as well:
use strict; use warnings; { # Outer block my $keeps_value = 0; sub mysub { print "$keeps_value\n"; $keeps_value++ } } # End of outer block mysub(); mysub(); mysub();
Produces:
0 1 2
The varibale $keeps_value is not visible outside the block.

Replies are listed 'Best First'.
Re^2: my variables in a subroutine
by Bloodnok (Vicar) on Mar 23, 2009 at 11:51 UTC
    Your example isn't a hack as such, it is, as lakshmananindia pointed out, a closure.

    A user level that continues to overstate my experience :-))
      Semantics, one man's hack is another's feature. I view it as a hack because it fulfills functionality missing from the language, i.e. a 'state' variable type (aka 'static') (and if it wasn't a missing feature then it wouldn't have been added).
        That's not a hack, it's a Design Pattern!