NAME

Math::Random::MT::Auto::Util - Utilities for Math::Random::MT::Auto subclasses

SYNOPSIS

### In Class ###

use Math::Random::MT::Auto::Util;

sub new
{
    ...

    my %args = extract_args( {
                                'PARAMS' => '/^(?:param|parm)s?$/i',
                                'OPTION' => '/^(?:option|opt)$/i',
                                'TYPE'   => '/^type$/i'
                             },
                             @_ );
    ...

    return ($self);
}

### In Application ###

my %initializers = (
      'Option' => 'filter',
      'Type'   => 'integer',
      'Math::Random::MT::Auto' => { 'Src' => 'dev_random' },
);

my $obj = My::Random->new(\%initializers,
                          'parms' => [ 4, 12 ]);

DESCRIPTION

This module provides utilities that support the inside-out object model used by Math::Random::MT::Auto.

extract_args
my %args = extract_args( { 'OPTION' => 'REGEX', ... }, @_ );

This subroutine provides a powerful and flexible mechanism for subclass constructors to accept arguments from application code, and to extract the arguments they need. It processes the argument list sent to the constructor, extracting arguments based on regular expressions, and returns a hash of the matches.

The arguments are presented to the constructor in any combination of key => value pairs and/or hash refs. These are combined by extract_args into a single hash from which arguments are extracted, and returned to the constructor.

Additionally, hash nesting is supported for providing class-specific arguments. For this feature, a key that is the name of a class is paired with a hash reference containing arguments that are meant for that class's constructor.

my $obj = My::Class::Sub::Whatever->new(
                'param'         => 'value',
                'My::Class'     => {
                                        'param' => 'item',
                                   },
                'My::Class:Sub' => {
                                        'param' => 'property',
                                   },
          );

In the above, class My::Class::Sub::Whatever will get 'param' => 'value', My::Class::Sub will get 'param' => 'property', and My::Class will get 'param' => 'item'.

The first argument to extract_args is a hash ref containing specifications for the arguments to be extracted. The keys in this hash will be the keys in the returned hash for any extracted arguments. The values are regular expressions that are used to match the incoming

DIAGNOSTICS

  • Usage: extract_args({ ARG=>\'REGEX\', ... }, @_)

    Your call to extract_args did not have a hash ref as its first argument.

  • Bad initializer: XXX ref not allowed. (Must be 'key=>val' pair or hash ref.)

  • Bad initializer: Missing value for key 'XXX'. (Must be 'key=>val' pair or hash ref.)

  • Class initializer for 'XXX' must be a hash ref

SEE ALSO

Inside-out Object Model: http://www.perlmonks.org/index.pl?node_id=219378, http://www.perlmonks.org/index.pl?node_id=483162, Chapter 15 of Perl Best Practices by Damian Conway, and Class::Std::Utils

AUTHOR

Jerry D. Hedden, <jdhedden AT 1979 DOT usna DOT com>

COPYRIGHT AND LICENSE

Copyright 2005 Jerry D. Hedden. All rights reserved.

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