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

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

Thread Previous | Thread Next
From:
Rafael Garcia-Suarez
Date:
April 2, 2007 05:17
Subject:
Re: Differences between state variables and smart matches in 5.10 and 6
Message ID:
b77c1dce0704020517l45f2f0a1o5c0bec663387e44a@mail.gmail.com
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).

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.

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