note
revdiablo
<p>I really want to reply here, but I am having a hard time thinking of a good example. It's not that I wouldn't use <code>state</code>, but that I would use it so often and instinctively that I can't really come up with anything of note. It would be like asking me for a "good example" of using the assignment operator. It's the kind of subconscious thing where I just use it when I need it.</p>
<p>That said, you [<em><strong>update</strong>: and [mirod] -- I must have missed his reply</em>] already described the general circumstances when I would use <code>state</code>. Basically, any time I would reach for a bare-block-scoped lexical to create a closure. So instead of the Perl5esque:</p>
<code>
{
my $foo;
sub foo {
$foo += @_;
}
}
</code>
<p>I'd use:</p>
<code>
sub foo {
state $foo;
$foo += @_;
}
</code>
<p>Which -- while maybe not a big win -- I think is pretty nice.</p>
<p><strong>Update</strong>: [BUU] makes a good point.</p>
473561
473561