Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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.


Comment on Re: How will you use state declared variables in Perl6?
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://473590]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2015-07-06 10:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (71 votes), past polls