perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daisuke Maki <dais...@wafu.ne.jp>
Subject Re: Attributes of two different instances of an object holding the same data
Date Wed, 07 Aug 2002 22:34:38 GMT

> What am I missing here?  After the 1st request finishes, the first 
> RegionSummary object should go out of scope and be destroyed, right? 

Well yeah, the OBJECT goes out of scope, but your "static" variable 
%_attr_data doesn't.

When you call

    $self->{$attrname} = $self->_default_for($attrname);

You are fetching a REFERENCE to an empty array in %_attr_data. That 
means what the object is pointing to and what %_attr_data points to are 
the same things. Thus when you assign that to the object, the array in 
%_attr_data is affected whenever you make change to the object's _region 
field.

I think what you may need to do is something like this (very much untested):

    use Storable qw/ dclone /;
    sub _default_for
    {
        my( $self, $attr ) = @_;
        my $value = $_attr_data{$attr}[0];

        ## I'm sure there are better ways, but what the hey...
        dclone($value);
    }

This way you get a cloned copy. If your default values are always going 
to be one of undef, regular scalar, array, or hash, then you could just 
switch on ref($value) and do something like

    if( ref( $value )  eq 'ARRAY' ) {
       my @ret = @$value;
       return \@ret;
    } elsif( ref( $value ) ....



--d


Mime
View raw message