On Sat, 4 Dec 2004 11:33:10 -0800, Larry Wall <[EMAIL PROTECTED]> wrote:
On Sat, Dec 04, 2004 at 08:03:45PM +0300, Alexey Trofimenko wrote:
: P.S.
: btw, what about
:
:   my @rray;
:   # i'm starting to like that "sigil is a part of name" idea :)
:   for 1..10 {
:      {
:        push @rray, \( state $calar )
:      }
:   }
:
:   say @rray[0] == @rray[1]

still your answer doesn't make it clearer. would this print 1 or undef?
I assumed it would say undef, but you said that state vars are reallocated only on function cloning, whatever it is.. so state vars are owned by functions, not bare blocks. If so, then it DOES print 1, doesn't it?


ah, that was a bad example, I can see now..

I thought, its primary use is for closures:

  sub test {
    my $a=10;
    return sub { $a++ }
  }

vs
  sub test {
    return sub {state $a=10; $a++ }
  }

$func1 = test;
$func2 = test;

would closures in $func1 and $func2 share the SAME $a?
or is it that "function cloning" you said?

oh! that it. I've found example which could make it clear to me

sub test {
  return sub {
    for 1..3 {
       state $var = 1;
       print $var++
    }
  }
}

$a = test; $a() for 1..3; print ';'
$b = test; $b() for 1..3;

that could print, for different possible definitions of "state":
1) 123123123;123123123
2) 123456789;123456789
3) 123456789;101112131415161718

looks like you meant third(!) variant.. but it doesn't make any sense for me.

Reply via email to