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 scalar, array or hash 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.
42 use Variable::Magic 0.51;
59 Temporarily replaces the value of the lexical or global variable C<$var> by C<$value> (respectively C<@var> by C<@value>, C<%var> by C<%value>), or by C<undef> if C<$value> is omitted (respectively empties C<@var> and C<%var> if the second argument is omitted), until the end of the current scope.
60 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.
61 Several C<temp> calls can be made onto the same variable, and the restore are processed in reverse order.
63 Note that destructors associated with the variable will B<not> be called when C<temp> sets the temporary value, but only at the natural end of life of the variable.
64 They will trigger after any destructor associated with the replacement value.
66 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.
67 If you want your code to run on these versions of C<perl>, you are encouraged to use L</set_temp> instead.
72 $wiz = Variable::Magic::wizard(
73 data => sub { $_[1] },
75 my ($token, $var) = @_;
76 &Variable::Magic::dispell($token, $wiz);
77 if (ref $var eq 'ARRAY') {
85 my ($token, $var) = @_;
86 # We need Variable::Magic 0.51 so that dispell in free does not crash.
87 &Variable::Magic::dispell($token, $wiz);
88 if (ref $var eq 'ARRAY') {
96 sub temp (\[$@%]) :lvalue {
98 my $target = Scope::Upper::UP;
101 if ($type eq 'ARRAY') {
103 &Scope::Upper::reap(sub { @$var = @save } => $target);
105 Variable::Magic::cast($token, $wiz, $var);
107 } elsif ($type eq 'HASH') {
109 &Scope::Upper::reap(sub { %$var = %save } => $target);
111 Variable::Magic::cast($token, $wiz, $var);
113 } else { # $type eq 'SCALAR' || $type eq 'REF'
115 &Scope::Upper::reap(sub { $$var = $save } => $target);
125 set_temp $var => $value;
128 set_temp @var => \@value;
131 set_temp %var => \%value;
133 A non-lvalue variant of L</temp> that can be used with any version of C<perl>.
137 sub set_temp (\[$@%];$) {
139 my $target = Scope::Upper::UP;
141 if ($type eq 'ARRAY') {
143 &Scope::Upper::reap(sub { @$var = @save } => $target);
144 @$var = @_ >= 2 ? @{$_[1]} : ();
145 } elsif ($type eq 'HASH') {
147 &Scope::Upper::reap(sub { %$var = %save } => $target);
148 %$var = @_ >= 2 ? %{$_[1]} : ();
149 } else { # $type eq 'SCALAR' || $type eq 'REF'
151 &Scope::Upper::reap(sub { $$var = $save } => $target);
159 The functions L</temp> and L</set_temp> are only exported on request by specifying their names in the module import list.
166 our %EXPORT_TAGS = ();
167 our @EXPORT_OK = qw<temp set_temp>;
173 L<Exporter> (core since perl 5).
177 L<Variable::Magic> 0.51.
187 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
189 You can contact me by mail or on C<irc.perl.org> (vincent).
193 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>.
194 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
198 You can find documentation for this module with the perldoc command.
200 perldoc Variable::Temp
202 =head1 COPYRIGHT & LICENSE
204 Copyright 2015,2017 Vincent Pit, all rights reserved.
206 This program is free software; you can redistribute it and/or modify it
207 under the same terms as Perl itself.
211 1; # End of Variable::Temp