On Tue, Dec 6, 2011 at 3:55 PM, Desilets, Alain
<alain.desil...@nrc-cnrc.gc.ca> wrote:
> No, I meant more things like this:
>
> -----------
>
> package MyClass;
>
> my $class_level_attribute;
>
> sub new {
>        my ($class) = @_;
>        $self->{instance_level_attribute} = undef;
>        bless $self, $class;
>        return $self;
> }

Well, in this case, $lcass_level_attribute is technically not
persistent, because you didn't use it inside your sub.  If you had
though, it would be persistent, just like my example.  Both would be
closures.

> This is something that I use sparingly (for example, to track how many 
> instances of a particular class are generated) and AFAIK, it's not considered 
> "bad manners". But it seems like it could get me in trouble in the context of 
> mod_perl.

It's fine to do that, but persistent is the desired effect there
because it's class-level data, so it shouldn't be unexpected.

> Note that the above not exactly the same as a static attribute in Java, in 
> the sense that the instance does not actually "inherit" the attribute. In 
> other words, you can't access $class_level_attribute by looking at 
> $self->{class_level_attribute}. But it's the closest thing I have found in 
> Perl for emulating Java style static class attributes.

If you really want that, you should look at Moose on CPAN.

- Perrin

Reply via email to