You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@perl.apache.org by Stas Bekman <st...@stason.org> on 2002/12/05 04:27:18 UTC

weird magic dependencies in custom directives merging

because ap_location_walk is called twice during the request, the 
DIR_MERGE is called twice as well. The problem I have is that in the 
following code $base magically gets affected by the return value from 
DIR_MERGE.

Consider:

sub DIR_MERGE {
     my($base, $add) = @_;
     use Data::Dumper;
     warn Dumper $base, $add;

     my %new = ();

     #push @{ $new{$_} }, @{ $base->{$_} } for keys %$base;
     # XXX: why this approach doesn't work?
     @new{keys %$base} = (values %$base);

     push @{ $new{$_} }, @{ $add->{$_}||[] } for keys %$add;

     warn Dumper \%new;
     return bless \%new, ref($base);
}

If I copy the values from %$base to %new via:

     @new{keys %$base} = (values %$base);

when DIR_MERGE is called second time, %$base becomes %new from the first 
call. Only if I go and extract SVs one by one:

     push @{ $new{$_} }, @{ $base->{$_} } for keys %$base;

$base doesn't get affected by %new.

See the new test TestDirectives/perlloadmodule2.pm for details, comment 
out the Dumper statements and see XXX.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: weird magic dependencies in custom directives merging

Posted by Stas Bekman <st...@stason.org>.
Stas Bekman wrote:
> because ap_location_walk is called twice during the request, the 
> DIR_MERGE is called twice as well. The problem I have is that in the 
> following code $base magically gets affected by the return value from 
> DIR_MERGE.
> 
> Consider:
> 
> sub DIR_MERGE {
>     my($base, $add) = @_;
>     use Data::Dumper;
>     warn Dumper $base, $add;
> 
>     my %new = ();
> 
>     #push @{ $new{$_} }, @{ $base->{$_} } for keys %$base;
>     # XXX: why this approach doesn't work?
>     @new{keys %$base} = (values %$base);

OK, Devel::Peek is a good friend and now I have it all sorted out. Since 
in my new test values of the base/add objects were references, copying a 
reference and then changing the array it was pointing to was obviously 
affecting the original object, which was getting wrong values when 
DIR_MERGE was called on the second time. That simple.

I'll document this issue.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org