NAME

Object::HashBase - Build hash based classes.

SYNOPSIS

A class:

package My::Class;
use strict;
use warnings;

# Generate 3 accessors
use Object::HashBase qw/foo -bar ^baz/;

# Chance to initialize defaults
sub init {
    my $self = shift;    # No other args
    $self->{+FOO} ||= "foo";
    $self->{+BAR} ||= "bar";
    $self->{+BAZ} ||= "baz";
}

sub print {
    print join ", " => map { $self->{$_} } FOO, BAR, BAZ;
}

Subclass it

package My::Subclass;
use strict;
use warnings;

# Note, you should subclass before loading HashBase.
use base 'My::Class';
use Object::HashBase qw/bat/;

sub init {
    my $self = shift;

    # We get the constants from the base class for free.
    $self->{+FOO} ||= 'SubFoo';
    $self->{+BAT} ||= 'bat';

    $self->SUPER::init();
}

use it:

package main;
use strict;
use warnings;
use My::Class;

# These are all functionally identical
my $one   = My::Class->new(foo => 'MyFoo', bar => 'MyBar');
my $two   = My::Class->new({foo => 'MyFoo', bar => 'MyBar'});
my $three = My::Class->new(['MyFoo', 'MyBar']);

# Accessors!
my $foo = $one->foo;    # 'MyFoo'
my $bar = $one->bar;    # 'MyBar'
my $baz = $one->baz;    # Defaulted to: 'baz'

# Setters!
$one->set_foo('A Foo');

#'-bar' means read-only, so the setter will throw an exception (but is defined).
$one->set_bar('A bar');

# '^baz' means deprecated setter, this will warn about the setter being
# deprecated.
$one->set_baz('A Baz');

$one->{+FOO} = 'xxx';

DESCRIPTION

This package is used to generate classes based on hashrefs. Using this class will give you a new() method, as well as generating accessors you request. Generated accessors will be getters, set_ACCESSOR setters will also be generated for you. You also get constants for each accessor (all caps) which return the key into the hash for that accessor. Single inheritance is also supported.

INCLUDING IN YOUR DIST

If you want to use HashBase, but do not want to depend on it, you can include it in your distribution.

$ hashbase_inc.pl Prefix::For::Module

This will create 2 files:

lib/Prefix/For/Module/HashBase.pm
t/HashBase.t

You can then use the includes Prefix::For::Module::HashBase instead of Object::HashBase.

You can re-run this script to regenerate the files, or upgrade them to newer versions.

If the script was not installed, it can be found int he scripts/ directory.

METHODS

PROVIDED BY HASH BASE

HOOKS

ACCESSORS

READ/WRITE

To generate accessors you list them when using the module:

use Object::HashBase qw/foo/;

This will generate the following subs in your namespace:

READ ONLY

use Object::HashBase qw/-foo/;

DEPRECATED SETTER

use Object::HashBase qw/^foo/;

SUBCLASSING

You can subclass an existing HashBase class.

use base 'Another::HashBase::Class';
use Object::HashBase qw/foo bar baz/;

The base class is added to @ISA for you, and all constants from base classes are added to subclasses automatically.

GETTING A LIST OF ATTRIBUTES FOR A CLASS

Object::HashBase provides a function for retrieving a list of attributes for an Object::HashBase class.

SOURCE

The source code repository for HashBase can be found at http://github.com/Test-More/HashBase/.

MAINTAINERS

AUTHORS

COPYRIGHT

Copyright 2017 Chad Granum exodist@cpan.org.

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

See http://dev.perl.org/licenses/