David Baird wrote:
No, the 'reference' to the lexical variable doesn't need to be a 'Perl reference', just some mention of the variable in the code. That will make it stick around, but only within the lexical scope specified.
David is correct. This is called a closure. Any use of a lexical variable declared OUTSIDE of the scope of the sub where you use it will make it permanent for that sub. Read up on closures for more.
Here's an example: my $counter = 1; sub print_counter { print $counter; $counter++; } The $counter variable will never go out of scope for this sub. - Perrin