Test2::Util::ExternalMeta(3p) Perl Programmers Reference Guide #
Test2::Util::ExternalMeta(3p) Perl Programmers Reference Guide
NNAAMMEE #
Test2::Util::ExternalMeta - Allow third party tools to safely attach
meta-data to your instances.
DDEESSCCRRIIPPTTIIOONN #
This package lets you define a clear, and consistent way to allow third
party tools to attach meta-data to your instances. If your object
consumes this package, and imports its methods, then third party meta-
data has a safe place to live.
SSYYNNOOPPSSIISS #
package My::Object;
use strict;
use warnings;
use Test2::Util::ExternalMeta qw/meta get_meta set_meta delete_meta/;
...
Now to use it:
my $inst = My::Object->new;
$inst->set_meta(foo => 'bar');
my $val = $inst->get_meta('foo');
WWHHEERREE IISS TTHHEE DDAATTAA SSTTOORREEDD?? #
This package assumes your instances are blessed hashrefs, it will not
work if that is not true. It will store all meta-data in the "_meta" key
on your objects hash. If your object makes use of the "_meta" key in its
underlying hash, then there is a conflict and you cannot use this
package.
EEXXPPOORRTTSS #
$val = $obj->meta($key)
$val = $obj->meta($key, $default)
This will get the value for a specified meta $key. Normally this will
return "undef" when there is no value for the $key, however you can
specify a $default value to set when no value is already set.
$val = $obj->get_meta($key)
This will get the value for a specified meta $key. This does not have
the $default overhead that "meta()" does.
$val = $obj->delete_meta($key)
This will remove the value of a specified meta $key. The old $val
will be returned.
$obj->set_meta($key, $val)
Set the value of a specified meta $key.
MMEETTAA--KKEEYY RREESSTTRRIICCTTIIOONNSS #
Meta keys must be defined, and must be true when used as a boolean. Keys
may not be references. You are free to stringify a reference "$ref" for
use as a key, but this package will not stringify it for you.
SSOOUURRCCEE #
The source code repository for Test2 can be found at
_h_t_t_p_:_/_/_g_i_t_h_u_b_._c_o_m_/_T_e_s_t_-_M_o_r_e_/_t_e_s_t_-_m_o_r_e_/.
MMAAIINNTTAAIINNEERRSS #
Chad Granum <exodist@cpan.org>
AAUUTTHHOORRSS #
Chad Granum <exodist@cpan.org>
CCOOPPYYRRIIGGHHTT #
Copyright 2020 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 _h_t_t_p_:_/_/_d_e_v_._p_e_r_l_._o_r_g_/_l_i_c_e_n_s_e_s_/
perl v5.36.3 2023-02-15 Test2::Util::ExternalMeta(3p)