Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Well, they aren't really quite the same as C's statics. You should really think of them more as persistent lexicals that also happen to know how to initialize themselves on first use. Unlike globals (and like lexicals) they are by default per-thread and per-clone. (They are not per-instance, even if used in methods. In a sense, "has" variables are state variables on the instance.)

If used in a class, the state variable does not generate a public accessor unless there is a dot twigil to indicate that. Without the accessor no other class can see the variable, not even derived classes. Only your class's methods can see it, and can therefore adjudicate any use of it by derived classes. The state belongs to your class. Given that (in general) class closures are not repeatedly cloned, there's really little to choose from between "my" and "state" in the class definition itself.

A given method may certainly declare a state variable and adjudicate its use as well. The state variable functions as a class variable that no other method can see, so it could be useful in the rare case that your method doesn't happen to trust the other methods in the class. Other than that, yes, a class attribute probably makes more sense.


In reply to Re: How will you use state declared variables in Perl6? by TimToady
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 browsing the Monastery: (4)
As of 2024-04-24 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found