use strict; use warnings; test() for (1..3); { my $memory = 0; sub test{ print "Value of static var is ", $memory++, "\n"; } } __END__ Value of static var is 0 Value of static var is 1 Value of static var is 2