]> git.vpit.fr Git - perl/modules/Variable-Temp.git/blobdiff - lib/Variable/Temp.pm
Introduce set_temp(), the non-lvalue variant of temp()
[perl/modules/Variable-Temp.git] / lib / Variable / Temp.pm
index c0e2aef3208c23dfc307ffc8d393983564b86377..be72888ace01d7b421b3e291464b175232961c1a 100644 (file)
@@ -55,6 +55,9 @@ Several C<temp> calls can be made onto the same variable, and the restore are pr
 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).
 They will trigger after any destructor associated with the replacement C<$value>.
 
+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.
+If you want your code to run on these versions of C<perl>, you are encouraged to use L</set_temp> instead.
+
 =cut
 
 sub temp (\$) :lvalue {
@@ -64,9 +67,26 @@ sub temp (\$) :lvalue {
  $$var;
 }
 
+=head2 C<set_temp>
+
+    set_temp $var;
+    set_temp $var => $value;
+
+A non-lvalue variant of L</temp> that can be used with any version of C<perl>.
+
+=cut
+
+sub set_temp (\$;$) {
+ my $var  = $_[0];
+ my $save = $$var;
+ &Scope::Upper::reap(sub { $$var = $save } => Scope::Upper::UP);
+ $$var = $_[1] if @_ >= 2;
+ return;
+}
+
 =head1 EXPORT
 
-The function L</temp> is only exported on request by passing C<'temp'> to the module import list.
+The functions L</temp> and L</set_temp> are only exported on request by specifying their names in the module import list.
 
 =cut
 
@@ -74,7 +94,7 @@ use base 'Exporter';
 
 our @EXPORT      = ();
 our %EXPORT_TAGS = ();
-our @EXPORT_OK   = 'temp';
+our @EXPORT_OK   = qw<temp set_temp>;
 
 =head1 CAVEATS