1 package Variable::Temp;
10 Variable::Temp - Temporarily change the value of a variable.
25 use Variable::Temp 'temp';
37 This module provides an utility routine that can be used to temporarily change the value of a variable, until the end of the current scope is reached where the original value of the variable is restored.
38 It is similar to C<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.
51 Temporarily replace the value of the lexical or global variable C<$var> by C<$value>, or by C<undef> if C<$value> is omitted, until the end of the current scope.
52 Any subsequent assignments to C<$var> in the current (or any inferior) scope will not affect the original value which will be restored into the variable at scope end.
53 Several C<temp> calls can be made onto the same variable, and the restore are processed in reverse order.
55 Note that destructors associated with C<$var> will B<not> be called when C<temp> sets the temporary value, but only at the natural end of life of the variable (i.e. at the end of the scope).
56 They will trigger after any destructor associated with the replacement C<$value>.
58 Due to a shortcoming in the handling of the C<\$> prototype, which was addressed in C<perl> 5.14, the pseudo-statement C<temp $var = $value> will cause compilation errors on C<perl> 5.12.x and below.
59 If you want your code to run on these versions of C<perl>, you are encouraged to use L</set_temp> instead.
63 sub temp (\$) :lvalue {
66 &Scope::Upper::reap(sub { $$var = $save } => Scope::Upper::UP);
73 set_temp $var => $value;
75 A non-lvalue variant of L</temp> that can be used with any version of C<perl>.
82 &Scope::Upper::reap(sub { $$var = $save } => Scope::Upper::UP);
83 $$var = $_[1] if @_ >= 2;
89 The functions L</temp> and L</set_temp> are only exported on request by specifying their names in the module import list.
96 our %EXPORT_TAGS = ();
97 our @EXPORT_OK = qw<temp set_temp>;
101 Currently only applies to scalar variables.
109 L<Exporter> (core since perl 5).
119 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
121 You can contact me by mail or on C<irc.perl.org> (vincent).
125 Please report any bugs or feature requests to C<bug-variable-temp at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Variable-Temp>.
126 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
130 You can find documentation for this module with the perldoc command.
132 perldoc Variable::Temp
134 =head1 COPYRIGHT & LICENSE
136 Copyright 2015 Vincent Pit, all rights reserved.
138 This program is free software; you can redistribute it and/or modify it
139 under the same terms as Perl itself.
143 1; # End of Variable::Temp