/usr/share/perl5/Test2/Util/ExternalMeta.pm is in libtest2-perl 0.000025-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | package Test2::Util::ExternalMeta;
use strict;
use warnings;
use Carp qw/croak/;
sub META_KEY() { '_meta' }
our @EXPORT = qw/meta set_meta get_meta delete_meta/;
use base 'Exporter';
sub set_meta {
my $self = shift;
my ($key, $value) = @_;
validate_key($key);
$self->{+META_KEY} ||= {};
$self->{+META_KEY}->{$key} = $value;
}
sub get_meta {
my $self = shift;
my ($key) = @_;
validate_key($key);
my $meta = $self->{+META_KEY} or return undef;
return $meta->{$key};
}
sub delete_meta {
my $self = shift;
my ($key) = @_;
validate_key($key);
my $meta = $self->{+META_KEY} or return undef;
delete $meta->{$key};
}
sub meta {
my $self = shift;
my ($key, $default) = @_;
validate_key($key);
my $meta = $self->{+META_KEY};
return undef unless $meta || defined($default);
unless($meta) {
$meta = {};
$self->{+META_KEY} = $meta;
}
$meta->{$key} = $default
if defined($default) && !defined($meta->{$key});
return $meta->{$key};
}
sub validate_key {
my $key = shift;
return if $key && !ref($key);
my $render_key = defined($key) ? "'$key'" : 'undef';
croak "Invalid META key: $render_key, keys must be true, and may not be references";
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Test2::Util::ExternalMeta - Allow third party tools to safely attach meta-data
to your instances.
=head1 EXPERIMENTAL RELEASE
This is an experimental release. Using this right now is not recommended.
=head1 DESCRIPTION
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.
=head1 SYNOPSYS
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');
=head1 WHERE IS THE DATA STORED?
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 C<_meta> key on your
objects hash. If your object makes use of the C<_meta> key in its underlying
hash, then there is a conflict and you cannot use this package.
=head1 EXPORTS
=over 4
=item $val = $obj->meta($key)
=item $val = $obj->meta($key, $default)
This will get the value for a specified meta C<$key>. Normally this will return
C<undef> when there is no value for the C<$key>, however you can specfi a
C<$default> value to set when no value is already set.
=item $val = $obj->get_meta($key)
This will get the value for a specified meta C<$key>. This does not have the
C<$default> overhead that C<meta()> does.
=item $val = $obj->delete_meta($key)
This will remove the value of a specified meta C<$key>. The old C<$val> will be
returned.
=item $obj->set_meta($key, $val)
Set the value of a specified meta C<$key>.
=back
=head1 META-KEY RESTICTIONS
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 C<"$ref"> for use as a
key, but this package will not stringify it for you.
=head1 SOURCE
The source code repository for Test2 can be found at
F<http://github.com/Test-More/Test2/>.
=head1 MAINTAINERS
=over 4
=item Chad Granum E<lt>exodist@cpan.orgE<gt>
=back
=head1 AUTHORS
=over 4
=item Chad Granum E<lt>exodist@cpan.orgE<gt>
=back
=head1 COPYRIGHT
Copyright 2015 Chad Granum E<lt>exodist7@gmail.comE<gt>.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See F<http://dev.perl.org/licenses/>
=cut
|