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.
-
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.
|