2 Variable::Temp - Temporarily change the value of a variable.
8 use Variable::Temp 'temp';
19 This module provides an utility routine that can be used to temporarily
20 change the value of a scalar, array or hash variable, until the end of
21 the current scope is reached where the original value of the variable is
22 restored. It is similar to "local", except that it can be applied onto
23 lexicals as well as globals, and that it replaces values by copying the
24 new value into the container variable instead of by aliasing.
37 Temporarily replaces the value of the lexical or global variable $var by
38 $value (respectively @var by @value, %var by %value), or by "undef" if
39 $value is omitted (respectively empties @var and %var if the second
40 argument is omitted), until the end of the current scope. Any subsequent
41 assignments to this variable in the current (or any inferior) scope will
42 not affect the original value which will be restored into the variable
43 at scope end. Several "temp" calls can be made onto the same variable,
44 and the restore are processed in reverse order.
46 Note that destructors associated with the variable will not be called
47 when "temp" sets the temporary value, but only at the natural end of
48 life of the variable. They will trigger after any destructor associated
49 with the replacement value.
51 Due to a shortcoming in the handling of the "\$" prototype, which was
52 addressed in "perl" 5.14, the pseudo-statement "temp $var = $value" will
53 cause compilation errors on "perl" 5.12.x and below. If you want your
54 code to run on these versions of "perl", you are encouraged to use
59 set_temp $var => $value;
62 set_temp @var => \@value;
65 set_temp %var => \%value;
67 A non-lvalue variant of "temp" that can be used with any version of
71 The functions "temp" and "set_temp" are only exported on request by
72 specifying their names in the module import list.
77 Exporter (core since perl 5).
89 Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
91 You can contact me by mail or on "irc.perl.org" (vincent).
94 Please report any bugs or feature requests to "bug-variable-temp at
95 rt.cpan.org", or through the web interface at
96 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Variable-Temp>. I will
97 be notified, and then you'll automatically be notified of progress on
98 your bug as I make changes.
101 You can find documentation for this module with the perldoc command.
103 perldoc Variable::Temp
106 Copyright 2015 Vincent Pit, all rights reserved.
108 This program is free software; you can redistribute it and/or modify it
109 under the same terms as Perl itself.