<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Test2::Util::ExternalMeta;
use strict;
use warnings;

our $VERSION = '1.302194';


use Carp qw/croak/;

sub META_KEY() { '_meta' }

our @EXPORT = qw/meta set_meta get_meta delete_meta/;
BEGIN { require Exporter; our @ISA = qw(Exporter) }

sub set_meta {
    my $self = shift;
    my ($key, $value) = @_;

    validate_key($key);

    $self-&gt;{+META_KEY} ||= {};
    $self-&gt;{+META_KEY}-&gt;{$key} = $value;
}

sub get_meta {
    my $self = shift;
    my ($key) = @_;

    validate_key($key);

    my $meta = $self-&gt;{+META_KEY} or return undef;
    return $meta-&gt;{$key};
}

sub delete_meta {
    my $self = shift;
    my ($key) = @_;

    validate_key($key);

    my $meta = $self-&gt;{+META_KEY} or return undef;
    delete $meta-&gt;{$key};
}

sub meta {
    my $self = shift;
    my ($key, $default) = @_;

    validate_key($key);

    my $meta = $self-&gt;{+META_KEY};
    return undef unless $meta || defined($default);

    unless($meta) {
        $meta = {};
        $self-&gt;{+META_KEY} = $meta;
    }

    $meta-&gt;{$key} = $default
        if defined($default) &amp;&amp; !defined($meta-&gt;{$key});

    return $meta-&gt;{$key};
}

sub validate_key {
    my $key = shift;

    return if $key &amp;&amp; !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 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 SYNOPSIS

    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-&gt;new;

    $inst-&gt;set_meta(foo =&gt; 'bar');
    my $val = $inst-&gt;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&lt;_meta&gt; key on your
objects hash. If your object makes use of the C&lt;_meta&gt; key in its underlying
hash, then there is a conflict and you cannot use this package.

=head1 EXPORTS

=over 4

=item $val = $obj-&gt;meta($key)

=item $val = $obj-&gt;meta($key, $default)

This will get the value for a specified meta C&lt;$key&gt;. Normally this will return
C&lt;undef&gt; when there is no value for the C&lt;$key&gt;, however you can specify a
C&lt;$default&gt; value to set when no value is already set.

=item $val = $obj-&gt;get_meta($key)

This will get the value for a specified meta C&lt;$key&gt;. This does not have the
C&lt;$default&gt; overhead that C&lt;meta()&gt; does.

=item $val = $obj-&gt;delete_meta($key)

This will remove the value of a specified meta C&lt;$key&gt;. The old C&lt;$val&gt; will be
returned.

=item $obj-&gt;set_meta($key, $val)

Set the value of a specified meta C&lt;$key&gt;.

=back

=head1 META-KEY RESTRICTIONS

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&lt;"$ref"&gt; 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&lt;http://github.com/Test-More/test-more/&gt;.

=head1 MAINTAINERS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 AUTHORS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 COPYRIGHT

Copyright 2020 Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See F&lt;http://dev.perl.org/licenses/&gt;

=cut
</pre></body></html>