Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re^5: How will you use state declared variables in Perl6? by BrowserUk
in thread How will you use state declared variables in Perl6? by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-25 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found