- publishing free software manuals
Perl Language Reference Manual
by Larry Wall and others
Paperback (6"x9"), 724 pages
ISBN 9781906966027
RRP £29.95 ($39.95)

Sales of this book support The Perl Foundation! Get a printed copy>>>

Deprecated use of my() in false conditional

(D deprecated) You used a declaration similar to my $x if 0. There has been a long-standing bug in Perl that causes a lexical variable not to be cleared at scope exit when its declaration includes a false conditional. Some people have exploited this bug to achieve a kind of static variable. Since we intend to fix this bug, we don't want people relying on this behavior. You can achieve a similar static effect by declaring the variable in a separate block outside the function, eg

sub f { my $x if 0; return $x++ }


{ my $x; sub f { return $x++ } }

Beginning with perl 5.9.4, you can also use state variables to have lexicals that are initialized only once (see "Perl pragma to enable new features" (feature) in the Perl Library Reference Manual (Volume 1)):

sub f { state $x; return $x++ }
ISBN 9781906966027Perl Language Reference ManualSee the print edition