NAME
Util::H2O::More - like if bless
created accessors for you. Intended for hash reference-based Perl OOP only. This module uses Util::H2O::h2o
as the basis for actual object creation; but there's no reason other accessor makers couldn't have been used or can be used. I just really like h2o
. :-)
CURRENTLY EXPERIMENTAL
This is a new module and still exploring the value and presentation of the interface. It may change (until noted here otherwise); it may also hopefully attract more h2o
-based utility methods. h2o
has a lot of other options, currently the only one exposed via baptise_deeply
is the -recurse
flag; as far as I know this is unique among the hash to object
modules on CPAN.
SYNOPSIS
Creating a new module using baptise
instead of bless
, which means it includes accessors (thanks to Util::H2O::h2o
). Below is an example of a traditional Perl OOP class constructor using baptise
to define a set of default accessors, in addition to any that are created by virtue of the %opts
passed.
use strict;
use warnings;
package Foo::Bar;
# exports 'h2o' also
use Util::H2O::More qw/baptise/;
sub new {
my $pkg = shift;
my %opts = @_;
# replaces bless, defines default constructures and creates
# constructors based on what's passed into %opts
my $self = baptise \%opts, $pkg, qw/bar haz herp derpes/;
return $self;
}
1;
Then on a client script,
use strict;
use warnings;
use Foo::Bar;
my $foo = Foo::Bar->new(some => q{thing}, else => 4);
print $foo->some . qq{\n};
# set bar via default accessor
$foo->bar(1);
print $foo->bar . qq{\n};
# default accessors also available from the class defined
# above,
# $foo->haz, $foo->herp, $foo->derpes
# and from the supplied tuple,
# $foo->else
For more example, please look at the classes created for the unit tests contained in t/lib
. More examples may be forthcoming as this module matures.
DESCRIPTION
The primary method, baptise
, essentially provides the same interface as the core keyword bless
with an additional slurpy third parameter where one may specify a list of default accessors.
Why Was This Created?
The really short answer: because h2o
doesn't play nice inside of the traditional Perl OOP constructor (new
) idiom. This is not h2o
's fault. This is my fault for wanting to use it to do something it was never meant to do.
Implied above is that I wanted to maintain the usage pattern of bless
, but extend it to include the generation of accessors. I wanted a better bless.
The long answer...
h2o
is an deceptively powerful tool that, above all, makes it easy and fun to add accessors to ad hoc hash references that many Perl developers like to use and that get emitted, unblessed by many popular modules. For example, HTTP::Tiny
, Web::Scraper
, and the more common select% methods DBI
flavors implement.
The usage pattern of h2o
begs it to be able to support being used as a drop in replacement for bless
. However, this is not h2o
's original intent and it will not work as a better bless. But is does a fine job as serving as the basis for a better bless.
METHODS
baptise $hash_ref, $pkg, LIST
-
Takes the same first 2 parameters as
bless
; with the addition of a list that defines a set of default accessors that do not rely on the top level keys of the provided hash reference. baptise_deeply $hash_ref, $pkg, LIST
-
Like
baptise
, but creates accessors recursively for a nested hash reference. Usesh2o
's-recurse
flag.Note: The accessors created in the nested hashes are handled directly by
h2o
by utilizing the-recurse
flag. This means that they will necessarily be blessed using the unchangable behavior ofh2o
, which maintains the name space ofUtil::H2O::_$hash
even ifh2o
is passed with the-isa
and-class
flags, which are both utilized to achieve the effective outcome ofbaptise
andbastise_deeply
.
EXTERNAL METHODS
h2o
-
Because
Util::H2O::More
exportsh2o
as the basis for its operations,h2o
is also available without needing to qualify its full name space.
DEPENDENCIES
Requires Util::H2O
because this module is effectively a wrapper around h2o
.
It also uses the state
keyword, which is only available in perls >= 5.10.
BUGS
Yes, I mean maybe. Buyer beware.
LICENSE AND COPYRIGHT
Perl/perl
AUTHOR
Oodler 577 <oodler@cpan.org>