MooseX::Traits::SetScalarByRef - Wrap a ScalarRef attribute's accessors to re-use a reference
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"; }
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.
None by default.
Moose, Moose::Util::TypeConstraints
Alex Becker, <c a p f a n a-t g m x do_t d e>
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 (C) 2014 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.
To install MooseX::Traits::SetScalarByRef, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::Traits::SetScalarByRef
CPAN shell
perl -MCPAN -e shell install MooseX::Traits::SetScalarByRef
For more information on module installation, please visit the detailed CPAN module installation guide.