| Meta::Builder::Base(3pm) | User Contributed Perl Documentation | Meta::Builder::Base(3pm) |
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()>.
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:
%actions should contain "action =<gt" sub {...}> pairs for constructing actions (See add_action()).
%actions should contain "action =<gt" sub {...}> pairs for constructing actions (See add_action()).
%actions should contain "action =<gt" sub {...}> pairs for constructing actions (See add_action()).
sub {
my $self = shift;
my ( $data, $metric, $action, @args ) = @_;
...;
}
Action and hook methods are called when someone calls "$meta-<gt"metric_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.
Arguments are:
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.
| 2022-12-12 | perl v5.36.0 |