in reply to variable declaration

I have no idea what you're trying to do, since you didn't really tell us, so I'm going to ignore your code. The short answer is that yes, you can have variables local to a sub that last through multiple calls:

use strict; use warnings; { my $persistent; sub myFunc { return ++$persistent; }} print myFunc(); print myFunc(); print myFunc();

Replies are listed 'Best First'.
Re^2: variable declaration
by choroba (Cardinal) on Dec 13, 2011 at 11:24 UTC
    In newer Perls, you can also use
    use strict; use warnings; use feature qw/state/; sub myFunc { state $persistent; return $persistent++; } print myFunc(); print myFunc(); print myFunc();