in reply to persistent variables between subroutines (long)
The block creates a private scope, so that $static is available only to the inc() and dec() functions. Adding BEGIN before the block ensures that $static is initialized before the subroutines are called.#!/usr/local/bin/perl -w use strict; BEGIN { my $static = 0; sub inc { ++$static; } sub dec { --$static; } } $, = $\ = "\n"; print inc(), inc(), dec(), dec();
|
|---|