Tie::Array::Random - Generates random for different fetched indexes


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

Index


Code Index:

NAME

Top

Tie::Array::Random - Generates random for different fetched indexes

SYNOPSIS

Top

  use Tie::Array::Random;

  my @array;
  tie @array, 'Tie::Array::Random';

  my $a_random_number           = $array[1];
  my $an_other_random_number    = $array[200];

  $a_random_number == $array[1]; ## True

  ## Set random type
  tie %hash, 'Tie::Array::Random', { set=> 'alpha', min=>5, max=>5 }};

DESCRIPTION

Top

Tie::Array::Random generates a random number each time a different index 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

FETCHSIZE

Fetchs size

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::Array


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

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

$VERSION = '1.01';
@ISA = qw(Tie::Array);


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

    my $args = shift;

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

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


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

  $self->{__max} = $key if $key > $self->{__max} ;

  $self->{$key} = $val;
}

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

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


  $self->{__max} = $key if $key > $self->{__max} ;

  return $self->{$key};
}

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

  return $self->{__max};
}




1;
__END__