|
Class::Hash - Perl extension for hashes that look like classes |
Class::Hash - Perl extension for hashes that look like classes
use Class::Hash ALL_METHODS => 1;
$hash = Class::Hash->new(foo => 'bar');
print "foo: ",$hash->foo,"\n"; # prints "foo: bar"
$hash->hello = "World"; print "Hello ",$hash->hello,"!\n"; # prints "Hello World!"
# Other accessor methods
$hash->store("foo", "something else");
$foo = $hash->fetch("foo");
# Or just use it like a plain hash ref!
$stuff->{foo} = "whoa dude!";
This component provides a method-based interface to a hash. Occasionally, it's more convenient to have named methods to access a hash than hash keys. This module generalizes this behavior.
This component provides a method-based interface to a hash. Occasionally, it's more convenient to have named methods to access a hash than hash keys. This module generalizes this behavior. It tries to work the tied hash interface inside-out.
This module tries to do as much or as little for you as you want and provides a number of configuration options. The options allow you to determine what kind of interface the object has. The interface may also be altered after-the-fact. See OPTIONS for details.
use Class::Hash, you may specify any default options
that should be made available. By default, all options are off--giving you
the simplest set of features. The default options can be modified per-instance
and options can be modified after instantiation via options.
For more information on the options, see OPTIONS.
The second argument is also optional. It is a hash reference containing the optiosn to set on this instance of the hash. If not options are given, then the defaults set during import are used. FOr more information on the options, see OPTIONS.
NB: It should be noted that:
$hash = Class::Hash->new;
is not the same as:
$hash2 = $hash->new;
The first will be treated as a constructor and the second as an accessor.
$new_value
if specified.
It is possible to disable the named accessor syntax by setting the ``no_named_accessors'' option. See the OPTIONS section for details.
fetch($name)$name. This fetches the
current value stored in $name. This accessor is only available when the
``fetch'' option is set. See the OPTIONS section for details.
store($name) = $new_value$name. This sets the current
value to be stored in $name. This accessor is only available when the
``store'' option is set. See the OPTIONS section for details.
delete($name)$name. This method is only
available when the ``delete'' option is set. See the OPTIONS section for
details.
clear($hash)exists($name)undef. This method is only available when the ``exists'' option is set. See
OPTIONS for details.
each($hash)keys($hash)values($hash)options($hash)
There are two types of options that may be set on Class::Hash objects: method options and aggregate options. The method options determine the presence or absence of various methods that may be defined in the Class::Hash object--see BUGS because this isn't strictly correct. The aggregate options alter the settings of more than one other options.
It should be noted that there are two possible syntaxes for calling most of the
Class::Hash methods. The first is the typical object syntax and the other is a
class/object syntax. The object syntax is available for all methods but
options. However, the object syntax is only available when it is turned on
by the matching option. The class/object syntax (always listed second when both
are possible) is always available regardless of option settings--but is far
less pretty.
$bob = new Class::Hash(foo => 'bar'); $foo = $bob->foo; # works!
$fred = new Class::Hash(bar => 'foo', { no_named_accessors => 1 });
$bar = $fred->bar; ### <--- ERROR! Undefined subroutine &Class::Hash::bar called
fetch accessor.
store accessor.
delete method.
clear method.
exists method.
each method.
keys method.
values method.
All aggregate option names are in all caps to suggest that you're turning on or off lots of stuff at once. Aggregate options always work one way, they do not have the effect of turning some things on and some stuff off. This would be too confusing.
no_named_accessors, fetch, store,
delete, clear, exists, each, keys, and values.
fetch, store, delete, clear,
exists, each, keys, and values.
The nastiest part of this module is the way AUTOLOAD and other methods are
made available. All the methods defined that aren't named accessors (such as
fetch, store, delete, clear, etc.) are defined as subroutines
whether they are ``turned on'' via options or not. This won't make a difference
99% of the time as the methods Do-The-Right-Thing(tm). However, when attempting
to use can, everything will be screwed up.
I would like to modify the system to have the methods only defined per-instance, but that would require the ability to load and unload method definitions on-the-fly per-instance. Something that might be possible, but would require some very odd finagling to achieve it, so I've stuck with the It-Works-For-Me(tm) method or It-Works-If-You-Just-Use-It-And-Don't-Try-To-Be-Funny(tm) method. :-)
Another problem is that this is currently set to require Perl 5.8.0. I don't
know if this is really necessary, but I'm too lazy to find out right now.
Because of the lvalue attribute set on AUTOLOAD, it does require 5.7.1,
which is almost the same as requiring 5.8.0.
There are probably some nasty documentation bugs. I didn't go back through and carefully proofread the documentation after I changed the implementation mid-way through.
Andrew Sterling Hanenkamp, <hanenkamp@users.sourceforge.net>
Copyright 2003 by Andrew Sterling Hanenkamp
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
|
Class::Hash - Perl extension for hashes that look like classes |