NAME
MooseX::Attribute::Localize - localize attribute values within a scope
VERSION
version 0.0.1
SYNOPSIS
package Foo;
use Moose;
use MooseX::Attribute::Localize;
has 'bar' => (
traits => [ 'Localize' ],
is => 'rw',
handles => {
set_local_bar => 'localize'
},
);
my $foo = Foo->new( bar => 'a' );
print $foo->bar; # 'a'
{
my $sentinel = $foo->set_local_bar( 'b' );
print $foo->bar; # 'b'
$foo->bar('c');
print $foo->bar; # 'c'
}
print $foo->bar; # 'a'
DESCRIPTION
Attributes that are given the trait Localize
can handle a localize
delegation, which stashes away the current value of the attribute and replaces it with a local value, mimicking the behavior of Perl's own local
.
The delegated method returns a sentinel variable. Once this variable gets out of scope, the attribute returns to its previous value.
If the delegated method is called in a void context, a warning will be issued as the sentinel will immediately get out of scope, which turns the whole thing into a glorious no-op.
PROVIDED METHODS
localize( $new_value )
Localizes the attribute. If a $new_value
is provided, initializes the newly localized value to it.
The method returns a sentinel object that will return the attribute to its previous value once it gets out of scope. The method will warn if it is called in a void context (as the sentinel will immediately falls out of scope).
AUTHOR
Yanick Champoux <yanick@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2015 by Yanick Champoux.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.