On Sat, 12 Mar 2005 02:06:20 +0100 (MET), Louis Pouzin wrote: >I would like to make a library subroutine that maintains the value of some >local variables between successive calls. Is there such variable type in Perl ? > >As a substitute I've been using "use vars ...". It works, but those variables >may be clobbered from any part of a program. Is there a better way ?
Use a lexical, in a block around your subs you want to allow access. Like this: { my $count; sub get { return ++$count; } sub clear { undef $count; } } $\ = "\n"; for (1..5) { print get; } clear(); for (1..3) { print get; } Result: 1 2 3 4 5 1 2 3 -- Bart.