Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by BrowserUk (Patriarch)
on Jul 08, 2005 at 22:42 UTC ( [id://473597]=note: print w/replies, xml ) Need Help??


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

Could you also clarify the difference between state variables and local variables please? (Or have I missed the demise of the local declarator?)


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.
  • Comment on Re^2: How will you use state declared variables in Perl6?

Replies are listed 'Best First'.
Re^3: How will you use state declared variables in Perl6?
by TimToady (Parson) on Jul 08, 2005 at 22:55 UTC
    Well, technically, yes, you missed the demise of local, but only because we renamed it to temp and made it completely orthogonal to storage class declarations. You can temporize any variable, and it will restore itself at the end of the current lexical scope. You can also conditionally temporize a variable using let, and it resets at the block end only if the block fails. But these are operations on existing variables. Even in Perl 5, local never declares a variable, but merely temporarily changes the value of an existing global.

    And to reiterate, state variables are not globals. They're persistent lexicals. You can temporize them, but then you can temporize my variables as well. You can even temporize attribute accessors and not worry about whether the accessor represents a real attribute or is a wrapper around something else:

    temp $handle.autoflush = 1;

      Okay. The analogy with C statics was flawed. That makes sense.

      I should really have referred to our rather than local, but I think your explaination suffices.

      What happens if I do:

      sub bar { state $foo; ... my $code = sub qux { state $foo; }; ... }
    • Is this illegal?
    • Or do the two refer to the same thing?
    • Or does the inner $foo hide the outer $foo for the duration of that scope?

      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.
        If it's illegal, it's because you took the address of a named declaration, not because of the state declarations. But ignoring the "qux" (and the entire question of whether that should be legal) your use of state variables would certainly be legal. The inner $foo hides the outer $foo just as a "my" variable would. And every time you clone the inner closure you'll get a new copy of the inner state variable, and it will initialize itself the first time that clone is called. It implies the existence of some kind of persistent lexical pad, which is a certain amount of work for the implementation, but we really didn't see much benefit in duplicating the functionality of globals, and a lot of benefit in having a kind of "my" that doesn't reinitialize the second time through.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-24 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found