develooper Front page | perl.perl5.porters | Postings from April 2007

Re: Differences between state variables and smart matches in 5.10and 6

Thread Previous | Thread Next
From:
Elizabeth Mattijsen
Date:
April 2, 2007 05:39
Subject:
Re: Differences between state variables and smart matches in 5.10and 6
Message ID:
p06230934c236a78040a1@[192.168.56.5]
At 2:20 PM +0200 4/2/07, Rafael Garcia-Suarez wrote:
>On 02/04/07, Elizabeth Mattijsen <liz@dijkmat.nl> wrote:
>>What are ONCE blocks more than something like:
>>
>>my %done;
>>sub ONCE (&) {
>>      my ( $code ) = @_;
>>
>>      return if exists $done{$code};
>>
>>      $code->();
>>      $done{$code} = undef;
>>}
>>
>>my $variable;
>>ONCE { $variable = $_ } foreach 1..5;
>>print "variable = $variable\n";
>>=====================
>>variable = 1
>
>Those run the code once per thread. (if %done isn't shared).

Hmmm... if run before a thread starts, it will inherit.  If not run 
before a thread starts, it will run once inside the thread. 
Different threads may get different initializations.  What is wrong 
with that?


>START blocks (not ONCE, after verification in Synopsis 4) run once per
>closure. See t/op/state.t for an example of code with a state variable
>in a closure, that gets its own state at each closure generation.

Ah, that's a different kettle of fish...  ;-)  Thanks for the clarification...



Liz



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About