Tech Journal Back to Tech Journal

How can I simulate a static variable (like in C) in Perl?

	my $secret_val = 0;
	sub gimme_another {
		return ++$secret_val;

$secret_val now becomes unreachable by the outside world, but retains its value between calls to gimme_another.

This works fine, since $secret_val goes out of scope, but is not garbage-collected since it's still being referenced.

Last updated on 2006-08-02 14:00:00 -0700, by Shalom Craimer

Back to Tech Journal