Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: How will you use state declared variables in Perl6?

by revdiablo (Prior)
on Jul 08, 2005 at 22:28 UTC ( #473590=note: print w/replies, xml ) Need Help??

in reply to How will you use state declared variables in Perl6?

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 state, 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.

That said, you [update: and mirod -- I must have missed his reply] already described the general circumstances when I would use state. Basically, any time I would reach for a bare-block-scoped lexical to create a closure. So instead of the Perl5esque:

{ my $foo; sub foo { $foo += @_; } }

I'd use:

sub foo { state $foo; $foo += @_; }

Which -- while maybe not a big win -- I think is pretty nice.

Update: BUU makes a good point.

Replies are listed 'Best First'.
Re^2: How will you use state declared variables in Perl6?
by BUU (Prior) on Jul 09, 2005 at 02:47 UTC
    You say "maybe not a big win", but what about something like:
    foo(); { my $foo = 42; sub foo { print "Foo: $foo" } }
    Which I've done all too often.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://473590]
[choroba]: finddata SoPW

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2017-02-24 06:23 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (353 votes). Check out past polls.