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

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

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


in reply to Re^3: How will you use state declared variables in Perl6?
in thread How will you use state declared variables in Perl6?

If that's what he meant, this would be the equivalent Perl6 code, using state:

sub genclosure { return sub { state $bar; $bar++; } }

I'm still not seeing a difference in safety.


Comment on Re^4: How will you use state declared variables in Perl6?
Select or Download Code
Re^5: How will you use state declared variables in Perl6?
by TimToady (Parson) on Jul 08, 2005 at 22:27 UTC
    That's correct--state variables clone. I see a lot of needless handwringing here based on the faulty assumption that Perl's state variables are just like C's static variables. There's a reason we gave them a different name...
      TimToady,
      The intent of the meditation was to get people thinking about state variables and how they will be useful. I believe that isn't being undermined here even with my bad choice of explanation. Thank you for participating and setting the record straight. The discussion may be better off by starting on the wrong foot because it gets the misconceptions out of the way with the authorative corrections all in one place.

      Cheers - L~R

Re^5: How will you use state declared variables in Perl6?
by BrowserUk (Pope) on Jul 08, 2005 at 22:29 UTC
    I'm still not seeing a difference in safety.

    If Limbic~Region's analogy with C statics is accurate, state defines an single, absolute, static piece of ram. So, this:

    sub genclosure { my $bar; return sub { $bar++; } }

    And this:

    sub genclosure { return sub { state $bar; $bar++; } }

    are semantically quite different.

    In the former case, each time you generate a coderef by calling genclosure(), that coderef will close over a different instance of the lexical $bar.

    In the latter case, each coderef generated will reference the same, single, immovable, static piece of ram</code> labelled $bar.

    And further, references to state $bar; in other unrelated generator functions--in the same package, or a different packages, or a different modules--will all refer to that same static piece of ram.

    It is possible that state will be cleverer than that, and somehow be scoped by file or package or namespace, but then a) the analogy with C static falls through; b) the would seem to be considerable overlap with local $bar;.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (18)
As of 2014-07-31 14:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (249 votes), past polls