NAME
MooseX::Traits::SetScalarByRef - Wrap a ScalarRef attribute's accessors to re-use a reference
SYNOPSIS
package Local::Example;
use Moose;
use Moose::Util::TypeConstraints;
use MooseX::Traits::SetScalarByRef;
subtype 'TkRef', as 'ScalarRef';
coerce 'TkRef', from 'Str', via { my $r = $_; return \$r };
has _some_val => (
traits => [ 'MooseX::Traits::SetScalarByRef' ],
isa => 'TkRef',
init_arg => 'some_val',
default => 'default value',
handles => 1,
);
package main;
my $eg = Local::Example->new;
my $ref_addr = refaddr($eg->some_val);
$eg->some_val("new string");
my $refaddr_after_change = refaddr($eg->some_val);
if($ref_addr eq $refaddr_after_change) {
print "refaddr did not change";
}
DESCRIPTION
This module wraps a ScalarRef attribute's accessors to ensure that when the setter is called with a new ScalarRef (or something that can be coerced into a ScalarRef), rather that the usual set action happening, you copy the string stored in the new scalar into the old scalar.
EXPORT
None by default.
SEE ALSO
Moose, Moose::Util::TypeConstraints
AUTHORS
Alex Becker, <asb@cpan.org>
CONTRIBUTORS
Many thanks to tobyink. He basicaklly wrote all the code for this module and provided it on stackoverflow.com.
Thanks to rsrchboy and @ether for the valuable feedback in #moose on irc.perl.org.
Thanks to Matt S Trout for the motivation of creating this module: Sufficiently encapsulated ugly is indistinguable from beautiful.
COPYRIGHT AND LICENSE
Copyright (C) 2014-2021 by Alex Becker
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.16.3 or, at your option, any later version of Perl 5 you may have available.