On Tue, Jan 10, 2017 at 09:04:00AM +0100, Luca Ferrari wrote:
> Hi all,
> I guess it was in Modern Perl that I saw this deprecated idiom to
> simulate state <http://perldoc.perl.org/functions/state.html>
> variables:
> 
> sub foo{
>    my $initialized_once = 1 if 0;
>   ...
> }
> 
> Now, why is that working and initializing the variable the first time?
> I mean, each time such row is evaluated the condition is 0 = false, so
> the variable should not be initialized never.
> What am I missing?

You are missing a fairly complicated interaction between the
implementation details of variable handling at compile-time and
run-time.  The details are probably not important unless you want to
understand them in order to hack on the core, or just for interest.  In
that case you will be able to find out all the gory details by searching
the perl5-porters mailing list archives or doing some git archaeology.

The behaviour came about by accident, but then people found out that it
filled a hole in the language and started using it.  The important point
is that it was deprecated in perl-5.10.0 and will be removed in
perl-5.30.0 (in a couple of years).  As you note, the correct way to get
this behaviour nowadays is to use the "state" keyword.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to