The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::CompositeHash -

SYNOPSIS

EXPORTS

Nothing exported by default

DEPENDENCIES

This module requires these other modules and libraries:

    Error::Simple
    Error::Programatic
    Perl::Module
    Data::Hub::Util
    Data::Hub::Container

DESCRIPTION

EXAMPLES

This example will not abort:

  my $h1 = {a=>'a1'};
  my $h2 = {a=>'a2', b=>'b2'};
  my $h3 = {a=>'a3', b=>'b3', c=>'c3'};
  my $ch = Data::CompositeHash->new($h2);
  $ch->unshift($h1);
  $ch->push($h3);
  # We now have a composite stack with prescedence: $h1, $h2, $h3
  # Where $h2 is the default hash
  $ch->{a} = 'AA';
  $ch->{b} = 'BB';
  $ch->{c} = 'CC';
  $ch->{d} = 'DD';
  die unless $h1->{a} eq 'AA';
  die unless $h2->{b} eq 'BB';
  die unless $h3->{c} eq 'CC';
  die unless $h2->{d} eq 'DD'; # set on default hash b/c 'd' did not exist

This example will not abort:

  my $h1 = {a=>'a1'};
  my $h2 = {a=>'a2', b=>'b2'};
  my $h3 = {a=>'a3', b=>'b3', c=>'c3'};
  my $ch = Data::CompositeHash->new($h2);
  $ch->unshift($h1);
  $ch->push($h3);
  # We now have a composite stack with prescedence: $h1, $h2, $h3
  # Where $h2 is the default hash
  die unless $ch->{a} eq 'a1';
  die unless $ch->{b} eq 'b2';
  die unless $ch->{c} eq 'c3';
  die unless ref($ch->{'/'});

This example:

  my $h1 = {a=>'a1'};
  my $h2 = {a=>'a2', b=>'b2'};
  my $h3 = {a=>'a3', b=>'b3', c=>'c3'};
  my $ch = Data::CompositeHash->new($h2);
  $ch->unshift($h1);
  $ch->push($h3);
  join '', sort values %$ch;

will return:

  a1b2c3

This example will not abort:

  my $h1 = {a=>'a1'};
  my $h2 = {a=>'a2', b=>'b2'};
  my $h3 = {a=>'a3', b=>'b3', c=>'c3'};
  my $ch = Data::CompositeHash->new($h2);
  $ch->unshift($h1);
  $ch->push($h3);
  die unless exists $$ch{a};
  die unless exists $$ch{b};
  die unless exists $$ch{c};
  die if exists $$ch{d};

This example:

  my $h1 = {a=>'a1'};
  my $h2 = {a=>'a2', b=>'b2'};
  my $h3 = {a=>'a3', b=>'b3', c=>'c3'};
  my $ch = Data::CompositeHash->new($h2);
  $ch->unshift($h1);
  $ch->push($h3);
  delete $ch->{a};
  delete $ch->{b};
  delete $ch->{c};
  join '', sort values %$ch;

will return:

  a2b3

This example:

  my $h1 = {a=>'a1'};
  my $h2 = {a=>'a2', b=>'b2'};
  my $h3 = {a=>'a3', b=>'b3', c=>'c3'};
  my $ch = Data::CompositeHash->new($h2);
  $ch->unshift($h1);
  $ch->push($h3);
  %$ch = ();
  join '', sort values %$ch;

will return:

  a2b2

This example will return true:

  my $h2 = {a=>'a2', b=>'b2'};
  my $ch = Data::CompositeHash->new($h2);
  scalar(%$ch);

This example will return false:

  my $h2 = {a=>'a2', b=>'b2'};
  my $ch = Data::CompositeHash->new($h2);
  $ch->pop;
  scalar(%$ch);

This example:

  use Data::CompositeHash;
  my $h1 = {
    'a' => 'argon',
  };
  my $h2 = {
    'a' => 'apple',
    'b' => 'banana',
    'c' => 'cherry',
  };
  my $h3 = {
    'c' => 'cyan',
    'd' => 'dark brown',
  };
  my $result = '';
  # Create a composite hash, using $h2 as the default hash
  my $ch = Data::CompositeHash->new($h2);
  # Add $h1 as a hash which will overried values in $h2
  $ch->unshift($h1);
  # Add $h3 as a hash with the least prescedence
  $ch->push($h3);
  # Fetch some values
  $result .= $ch->{a} . "\n";
  $result .= $ch->{b} . "\n";
  $result .= $ch->{c} . "\n";
  $result .= $ch->{d} . "\n";
  # Set an ambiguous value
  $result .= "--\n";
  $ch->{a} = 'aluminium';
  $result .= $h1->{a} . "\n"; # contains new value
  $result .= $h2->{a} . "\n"; # not touched
  # Set an ambiguous value
  $result .= "--\n";
  $ch->{c} = 'cantelope';
  $result .= $h2->{c} . "\n"; # contains new value
  $result .= $h3->{c} . "\n"; # not touched
  # Setting a value which is not defined in any hash sets
  # it on the defalut hash
  $result .= "--\n";
  $ch->{e} = 'edible fruit';
  $result .= $h2->{e};

will return:

  argon
  banana
  cherry
  dark brown
  --
  aluminium
  apple
  --
  cantelope
  cyan
  --
  edible fruit

PUBLIC INTERFACE

new

Create a new CompositeHash

This example will not abort:

  use Data::CompositeHash;

This example will abort:

  # no default hash provided
  my $ch = Data::CompositeHash->new();

shift

Shift a hash from the composite stack

This example will not return a defined value:

  my $h2 = {a=>'a2'};
  my $ch = Data::CompositeHash->new($h2);
  $ch->shift;
  $ch->{a};

unshift

Unshift hash(es) on to the composite stack

This example:

  my $h1 = {a=>'a1'};
  my $h2 = {a=>'a2'};
  my $ch = Data::CompositeHash->new($h2);
  $ch->unshift($h1);
  $ch->{a};

will return:

  a1

push

Push hash(es) on to the composite stack

This example:

  my $h2 = {a=>'a2'};
  my $h3 = {a=>'a3'};
  my $ch = Data::CompositeHash->new($h2);
  $ch->push($h3);
  $ch->{a};

will return:

  a2

pop

Pop a hash off the composite stack

This example will not return a defined value:

  my $h2 = {a=>'a2'};
  my $ch = Data::CompositeHash->new($h2);
  $ch->pop;
  $ch->{a};

AUTHORS

    Ryan Gies <ryangies@cpan.org>

COPYRIGHT

    Copyright (C) 2014-2016 by Ryan Gies. All rights reserved.
    Copyright (C) 2006-2013 by Livesite Networks, LLC. All rights reserved.
    Copyright (C) 2000-2005 by Ryan Gies. All rights reserved.
    Redistribution and use in source and binary forms, with or without 
    modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright notice, 
    this list of conditions and the following disclaimer.
    * The origin of this software must not be misrepresented; you must not 
    claim that you wrote the original software. If you use this software in a 
    product, an acknowledgment in the product documentation would be 
    appreciated but is not required.
    * Altered source versions must be plainly marked as such, and must not be 
    misrepresented as being the original software.
    * The name of the author may not be used to endorse or promote products 
    derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 
    WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
    EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
    OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
    OF SUCH DAMAGE.
    To the best of our knowledge, no patented algorithms have been used. However, we
    do not have the resources to carry out a patent search, and therefore cannot 
    give any guarantee of the above statement.