NAME
MooseX::LocalAttribute - local-ize attributes on Moose-ish objects
SYNOPSIS
use MooseX::LocalAttribute;
my $freddy = Person->new( name => 'Freddy' );
print $freddy->name; # Freddy
{
my $temporary_name = 'Mr Orange';
my $guard = local_attribute( $freddy, "name", $temporary_name );
print $freddy->name; # Mr Orange
steal_diamonds( $freddy );
}
print $freddy->name; # Freddy
DESCRIPTION
This module provides a mechanism to temporarily replace the value of an object attribute with a different variable. In typical object oriented Perl code, an object contains a blessed hash reference, so it's possible to reach into the internals to localise data.
my $local_bar;
local $foo->{bar} = \$local_bar;
This has a few problems though. It is generally a better idea to use accessors rather than to rumage around in the internals of an object. This is especially true if one does not know whether the object is in fact a hash reference under the hood.
When a variable is localised with local
, a backup of that variable is made. Perl then places a directive on the stack that restores the variable when it is goes out of scope. This module does the same thing for attributes of objects.
WHICH OBJECTS DOES THIS WORK FOR
While this module is called MooseX::LocalAttribute, it will work for all kinds of objects, as long as there is a read/write accessor. It has been tested for Moose, Mouse, Moo, Mo, Mojo::Base, Class::Accessor, Util::H2O, Object::PAD and classic Perl OO code using bless
with hand-rolled accessors. There is a good chance it will work on other object implementations too.
EXPORTS
local_attribute($obj, $attr, $val)
Takes an object $obj
and temporarily localizes the attribute $attr
on it to $val
. It returns a Scope::Guard object that will restore the original value of $attr
when it goes out of scope.
my $guard = local_attribute( $bob, 'name', 'joe' ); # $bob->name eq 'joe'
You must always capture the return value of local_attribute
and store it in a variable. It will die if called in void context, because the underlying Scope::Guard object cannot work in void context. Your attribute would be replaced permanently.
local_attribute( $foo, 'attr', 'new value' ); # BOOM
This function is exported by default.
OBJECTS THIS DOES NOT WORK FOR
Class::Std - this does not support combined getter/setter methods
Object::Tiny - this creates read-only accessors
SEE ALSO
AUTHOR
Julien Fiegehenn <simbabque@cpan.org>
COPYRIGHT
Copyright (c) 2022, Julien Fiegehenn.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.