Tie::Hash::Vivify - Create hashes that autovivify in interesting ways.
use Tie::Hash::Vivify; my $default = 0; tie my %hash => 'Tie::Hash::Vivify', sub { "default" . $default++ }; print $hash{foo}; # default0 print $hash{bar}; # default1 print $hash{foo}; # default0 $hash{baz} = "hello"; print $hash{baz}; # hello my $hashref = Tie::Hash::Vivify->new(sub { "default" }); $hashref->{foo}; # default # ...
This module implements a hash where if you read a key that doesn't exist, it will call a code reference to fill that slot with a value.
You can either tie to the Tie::Hash::Vivify package:
Tie::Hash::Vivify
tie my %hash => 'Tie::Hash::Vivify', sub { "my default" };
Or you can create a new anonymous reference to a Tie::Hash::Vivify hash:
my $hashref = Tie::Hash::Vivify->new(sub { "my default" });
Luke Palmer, lrpalmer gmail com
Copyright (C) 2005 by Luke Palmer
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.
To install Tie::Hash::Vivify, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tie::Hash::Vivify
CPAN shell
perl -MCPAN -e shell install Tie::Hash::Vivify
For more information on module installation, please visit the detailed CPAN module installation guide.