Newcomen

Description A static content generator.
Newcomen > Perl Modules > Newcomen::Util::Hash
Source

Index


NAME ^

Newcomen::Util::Hash - Hash related utility functions.

SYNOPSIS ^

use Newcomen::Util::Hash;

# Merge two hashes:
my $merged = Newcomen::Util::Hash::merge ($hashref_1, $hashref_2);

DESCRIPTION ^

This package provides utility functions dealing with hashes. Note that all functions may be imported into the package using Newcomen::Util::Hash:

use Newcomen::Util::Hash qw( merge );

No function is exported by default.

FUNCTIONS ^

merge

Toggle source:   merge

sub merge {

   die 'Expected two hashrefs' unless ref $_ [0] eq 'HASH' and ref $_ [1] eq 'HASH' and $#_ == 1;

   my $left  = Clone::clone shift;
   my $right = Clone::clone shift;

   for my $key (keys %$right) {
      if (ref $left -> {$key} eq 'HASH' and ref $right -> {$key} eq 'HASH') {
         $left -> {$key} = merge ($left -> {$key}, $right -> {$key});
      }
      else {
         $left -> {$key} = $right -> {$key};
      }
   }

   return $left;

}
my $merged = merge ($hashref_1, $hashref_2);

This function basically works like Hash::Merge::Simple's merge() function. It will merge the two hashes (passed by their references) and return the result (as a hashref). It expects exactly two parameters. Before merging, the hashes will be cloned (using Clone's clone() function), so modifying the result will not alter the originals. Elements in the second hash will override those in the first hash if required. Do not put any circular hashref stuff in the hashes, it may not work!

SEE ALSO ^

Clone, Hash::Merge::Simple

VERSION ^

This is version 2014052501.

AUTHOR ^

Stefan Goebel - newcomen {at} subtype {dot} de

COPYRIGHT AND LICENSE ^

Copyright 2013-2014 Stefan Goebel.

This file is part of Newcomen.

Newcomen is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the license, or (at your option) any later version.

Newcomen is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Newcomen. If not, see <http://www.gnu.org/licenses/>.