On Fri, 4 Oct 2002, Michael Lazzaro wrote:

: Date: Fri, 4 Oct 2002 16:40:04 -0700
: From: Michael Lazzaro <[EMAIL PROTECTED]>
: To: [EMAIL PROTECTED]
: Subject: Draft Proposal: Declaring Classwide Attributes
: 
: (Disclaimer: My purpose in proposing this is not to recommend it, but 
: to document whether the idea should be endorsed, or shot down, and any 
: proposed canonical syntax.  Note that the later implications of these 
: choices are quite substantial.  Please discuss!)
: 
: [Draft Proposal: Declaring Classwide Attributes]
: 
: Within a class, "classwide attributes" are declared using the standard 
: "my" and "our".
: 
: Example:
: 
:       class Zap {
:               my %zap_cache;  # a private classwide attribute
:               our $zap_count = 0;     # a public classwide attribute
: 
:               attr $foo;
:               attr $bar;
:       }
: 
: 
: [Discussion]
: 
: Many OO-based languages have the concept of "classwide" attributes; 
: that is, attributes that only exist once, for the class (and all 
: subclasses?), as opposed to existing one for each instance of a class.  
: You can use these attributes as counters, or caches, or any other 
: common ground for use by all instances of the class.
: 
: Within a class definition, Perl simply uses the my/our keywords for 
: this purpose.
: 
: If any value is to be assigned to a "classwide" attributes, that 
: assignment is done once, upon initialization of the class.

If you want accessor methods, use the dot:

        class Zap {
                my %.zap_cache;         # a private classwide attribute
                our $.zap_count = 0;    # a public classwide attribute

                has $.foo;
                has $.bar;
        }

It may be that $.zap_count is public not so much because of the class definition
where it would default to private, but because $.zap_count's real global name is
$Zap::.zap_count.  To get a public accessor method you might still need to declare
it public.  And you could get a public accessor method to the "my" variable as well
the same way.  (That means is that the {...} of the class definition is really just
a closure that executes once when the class is built.)

Larry

Reply via email to