From: Brian Cassidy Date: Fri, 23 Jun 2006 15:22:49 +0000 (+0000) Subject: ripped out the guts of merge_config_hashes and put it in a merge_hashes utility sub X-Git-Tag: 5.7099_04~468 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=358e159286ecfb8089814e4dfe069d8b7b6b95dd ripped out the guts of merge_config_hashes and put it in a merge_hashes utility sub --- diff --git a/lib/Catalyst/Component.pm b/lib/Catalyst/Component.pm index b38361e..d4cf7e5 100644 --- a/lib/Catalyst/Component.pm +++ b/lib/Catalyst/Component.pm @@ -3,6 +3,7 @@ package Catalyst::Component; use strict; use base qw/Class::Accessor::Fast Class::Data::Inheritable/; use NEXT; +use Catalyst::Utils; __PACKAGE__->mk_classdata($_) for qw/_config _plugins/; @@ -134,7 +135,7 @@ sub process { . " did not override Catalyst::Component::process" ); } -=head2 $c->merge_hash_config( $hashref, $hashref ) +=head2 $c->merge_config_hashes( $hashref, $hashref ) Merges two hashes together recursively, giving right-hand precedence. @@ -143,21 +144,7 @@ Merges two hashes together recursively, giving right-hand precedence. sub merge_config_hashes { my ( $self, $lefthash, $righthash ) = @_; - my %merged = %$lefthash; - for my $key ( keys %$righthash ) { - my $right_ref = ( ref $righthash->{ $key } || '' ) eq 'HASH'; - my $left_ref = ( ( exists $lefthash->{ $key } && ref $lefthash->{ $key } ) || '' ) eq 'HASH'; - if( $right_ref and $left_ref ) { - $merged{ $key } = $self->merge_config_hashes( - $lefthash->{ $key }, $righthash->{ $key } - ); - } - else { - $merged{ $key } = $righthash->{ $key }; - } - } - - return \%merged; + return Catalyst::Utils::merge_hashes( $lefthash, $righthash ); } =head1 OPTIONAL METHODS diff --git a/lib/Catalyst/Utils.pm b/lib/Catalyst/Utils.pm index ac20a70..c6710b2 100644 --- a/lib/Catalyst/Utils.pm +++ b/lib/Catalyst/Utils.pm @@ -237,6 +237,34 @@ sub ensure_class_loaded { return 1; } +=head2 merge_hashes($hashref, $hashref) + +Base code to recursively merge two hashes together with right-hand precedence. + +=cut + +sub merge_hashes { + my ( $lefthash, $righthash ) = @_; + + return $lefthash unless defined $righthash; + + my %merged = %$lefthash; + for my $key ( keys %$righthash ) { + my $right_ref = ( ref $righthash->{ $key } || '' ) eq 'HASH'; + my $left_ref = ( ( exists $lefthash->{ $key } && ref $lefthash->{ $key } ) || '' ) eq 'HASH'; + if( $right_ref and $left_ref ) { + $merged{ $key } = merge_hashes( + $lefthash->{ $key }, $righthash->{ $key } + ); + } + else { + $merged{ $key } = $righthash->{ $key }; + } + } + + return \%merged; +} + =head1 AUTHOR