Re: Is it me or is mod_perl extremely dangerous?

Perrin Harkins <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <CAC0_be2PVOyQjKaUU1jbD=K+ba1gOxi22M78NkR5bS=_77EbDw@mail.gmail.com>
On Tue, Dec 6, 2011 at 3:55 PM, Desilets, Alain
<[email protected]> 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.