Tie::Hash::Random - Generates random for different fetched keys


Tie-Hash-Random documentation Contained in the Tie-Hash-Random distribution.

Index


Code Index:

NAME

Top

Tie::Hash::Random - Generates random for different fetched keys

SYNOPSIS

Top

  use Tie::Hash::Random;

  my %hash;
  tie %hash, 'Tie::Hash::Random';

  my $a_random_number           = $hash{foo};
  my $an_other_random_number    = $hash{bar};

  $a_random_number == $hash{foo}; ## True

  ## Set a seed
  tie %hash, 'Tie::Hash::Random', { set=> 'alpha', min=>5, max=>5 }};

DESCRIPTION

Top

Tie::Hash::Random generates a random number each time a different key is fetched.

The actual random data is generated using Data::Random rand_chars function. The default arguments are ( set => 'all', min => 5, max => 8 ) which can be modifed using tie parameters as shown in the SYNOPSIS.

STORE

Stores data

FETCH

Fetchs

FIRSTKEY

AUTHOR

Top

Matias Alejo Garcia <matiu@cpan.org>

UPDATES

Top

The latest version of this module will always be available from from CPAN at http://search.cpan.org/~ematiu.

COPYRIGHT

Top

LICENSE

Top

This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Top

perl(1), perltie(1), Tie::StdHash(1), Tie::Hash::Cannabinol, Data::Random


Tie-Hash-Random documentation Contained in the Tie-Hash-Random distribution.
package Tie::Hash::Random; 

use 5.006;
use strict;
use warnings;
use vars qw($VERSION @ISA);
use Tie::Hash;
use Data::Random qw(:all);

$VERSION = '1.02';
@ISA = qw(Tie::Hash);


sub TIEHASH  {
    my $storage = bless {}, shift;

    my $args = shift;

    $storage->{__rand_config} = { set => 'numeric', min => 5, max => 8 };

    foreach (keys %$args) {
        $storage->{__rand_config}->{$_} = $args->{$_};
    }
 
    return $storage;
}


sub STORE {
  my ($self, $key, $val) = @_;
  $self->{$key} = $val;
}

sub FETCH {
  my ($self, $key) = @_;

  $self->{$key} = join '', rand_chars( %{$self->{__rand_config}} ) if ! exists $self->{$key};

  return $self->{$key};
}


sub FIRSTKEY {
  my ($self) = @_;
  return 1;
}


1;
__END__