> > But declaring a variable as "my" is not sufficient to prevent it from being 
> > persistent. You can declare a package variable with "my" and it will 
> > persist.
>
> That's true!  I assume you mean this:
> 
> my $foo = 7;
>
> sub print_foo {
>     print $foo;
> }
>
> That's called a closure.  Don't do that unless you want persistence.
>
> Using locally declared variables in subs and passing in arguments
> instead of grabbing data directly from variables in a larger scope is
> good programming practice, and it's the way to avoid unwanted variable
> persistence in mod_perl and FastCGI.

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;
}
------------

The idea here is to emulate the concept of "static" (in Java or C++ parlance) 
class level attributes, i.e., attributes which are tied to the the class itself 
as opposed to instances of it.

In the above example, every instance of MyClass can have its own value of 
instance_level_attribute. But there is only one shared value of 
$class_level_attribute.

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.

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.

Alain

Reply via email to