Meta::Builder::Base - Base class for Meta::Builder Meta Objects.
Base class for all Meta::Builder Meta objects. This is where the methods used to define new metrics and actions live. This class allows for the creation of dynamic meta objects.
My/Meta.pm:
package My::Meta; use strict; use warnings; use base 'Meta::Builder::Base'; # Name the accessor that will be defined in the class that uses the meta object # It is used to retrieve the classes meta object. __PACKAGE__->set_accessor( "mymeta" ); # Add a metric with two actions __PACKAGE__->add_metric( mymetric => sub { [] }, pop => sub { my $self = shift; my ( $data ) = @_; pop @$data; }, push => sub { my $self = shift; my ( $data, $metric, $action, @args ) = @_; push @$data => @args; } ); # Add an additional action to the metric __PACKAGE__->add_action( 'mymetric', get_ref => sub { shift }); # Add some predefined metric types + actions __PACKAGE__->add_hash_metric( 'my_hashmetric' ); __PACKAGE__->add_lists_metric( 'my_listsmetric' );
My.pm:
package My; use strict; use warnings; use My::Meta; My::Meta->new( __PACKAGE__ ); # My::Meta defines mymeta() as the accessor we use to get our meta object. # this is the ONLY way to get the meta object for this class. mymeta()->mymetric_push( "some data" ); mymeta()->my_hashmetric_add( key => 'value' ); mymeta()->my_listsmetric_push( list => qw/valueA valueB/ ); # It works fine as an object/class method as well. __PACKAGE__->mymeta->do_thing(...); ...;
Whenever you create a new instance of a meta-object you must provide the name of the package to which the meta-object belongs. The 'package' metric will be set to this package name, and can be retrieved via the 'package' method: $meta-package()>.
$meta-
Hash metrics are metrics that hold key/value pairs. A hash metric is defined using either the hash_metric() function, or the $meta-add_hash_metric()> method. The following actions are automatically defined for hash metrics:
hash_metric()
Add a key/value pair to the metric. Will throw an exception if the metric already has a value for the specified key.
Get the value for a specified key.
Check that the metric has the specified key defined.
Clear the specified key/value pair in the metric. (returns nothing)
Get the value for the specified key, then clear the pair form the metric.
Push values into the specified list for the given metric.
Get the values for a specified key.
Check that the metric has the specified list.
Clear the specified list in the metric. (returns nothing)
Get the values for the specified list in the metric, then clear the list.
Create a new instance of the meta-class, and apply it to $package.
Get the meta data for the meta-class itself. (The meta-class is build using meta-data)
Generate a new empty hashref.
Generate the name of the method for the given metric and action. Override this if you do not like the METRIC_ACTION() method names.
Get the name of the package to which this meta-class applies.
Set the accessor that is used to retrieve the meta-object from the class to which it applies.
Add a hash metric (see "HASH METRICS").
%actions should contain action =<gt sub {...}> pairs for constructing actions (See add_action()).
action =<gt
Add a lists metric (see "LISTS METRICS")
Add a custom metric. The second argument should be a sub that generates a default value for the metric.
Add an action for the specified metric. See "ACTION AND HOOK METHODS" for details on how to write an action coderef.
Add a hook for the specified metric. See "ACTION AND HOOK METHODS" for details on how to write a hook coderef.
sub { my $self = shift; my ( $data, $metric, $action, @args ) = @_; ...; }
Action and hook methods are called when someone calls $meta-<gtmetric_action(...)>. First all before hooks will be called, the action itself, and finally the after hooks will be called. All methods in the chain get the exact same unaltered arguments. Only the main action sub can return anything.
$meta-<gt
Arguments are:
These are methods, so the first argument is the meta object itself.
This is the data structure stored for the metric. This is the same as calling $meta->metric()
Name of the metric
Name of the action
Arguments that metric_action() was called with.
There are the default action methods used by hashmetrics and listsmetrics.
Chad Granum exodist7@gmail.com
Copyright (C) 2010 Chad Granum
Meta-Builder is free software; Standard perl licence.
Meta-Builder is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
To install Meta::Builder, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Meta::Builder
CPAN shell
perl -MCPAN -e shell install Meta::Builder
For more information on module installation, please visit the detailed CPAN module installation guide.