Variable::Temp - Temporarily change the value of a variable.
Version 0.04
use Variable::Temp 'temp'; my $x = 1; say $x; # 1 { temp $x = 2; say $x; # 2 } say $x; # 1
This module provides an utility routine that can be used to temporarily change the value of a scalar, array or hash variable, until the end of the current scope is reached where the original value of the variable is restored. It is similar to local, except that it can be applied onto lexicals as well as globals, and that it replaces values by copying the new value into the container variable instead of by aliasing.
local
temp
temp $var; temp $var = $value; temp @var; temp @var = \@value; temp %var; temp %var = \%value;
Temporarily replaces the value of the lexical or global variable $var by $value (respectively @var by @value, %var by %value), or by undef if $value is omitted (respectively empties @var and %var if the second argument is omitted), until the end of the current scope. Any subsequent assignments to this variable in the current (or any inferior) scope will not affect the original value which will be restored into the variable at scope end. Several temp calls can be made onto the same variable, and the restore are processed in reverse order.
$var
$value
@var
@value
%var
%value
undef
Note that destructors associated with the variable will not be called when temp sets the temporary value, but only at the natural end of life of the variable. They will trigger after any destructor associated with the replacement value.
Due to a shortcoming in the handling of the \$ prototype, which was addressed in perl 5.14, the pseudo-statement temp $var = $value will cause compilation errors on perl 5.12.x and below. If you want your code to run on these versions of perl, you are encouraged to use "set_temp" instead.
\$
perl
temp $var = $value
set_temp
set_temp $var; set_temp $var => $value; set_temp @var; set_temp @var => \@value; set_temp %var; set_temp %var => \%value;
A non-lvalue variant of "temp" that can be used with any version of perl.
The functions "temp" and "set_temp" are only exported on request by specifying their names in the module import list.
perl 5.6.
Exporter (core since perl 5).
Scope::Upper.
Variable::Magic 0.51.
"local" in perlfunc.
Vincent Pit, <perl at profvince.com>, http://www.profvince.com.
<perl at profvince.com>
You can contact me by mail or on irc.perl.org (vincent).
irc.perl.org
Please report any bugs or feature requests to bug-variable-temp at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Variable-Temp. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-variable-temp at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Variable::Temp
Copyright 2015,2017 Vincent Pit, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Variable::Temp, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Variable::Temp
CPAN shell
perl -MCPAN -e shell install Variable::Temp
For more information on module installation, please visit the detailed CPAN module installation guide.