From: Vincent Pit Date: Thu, 12 Mar 2015 15:05:25 +0000 (-0300) Subject: Introduce set_temp(), the non-lvalue variant of temp() X-Git-Tag: v0.02~14 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=6ff9c73813cc7b7f722c2693d0a0aea4a526b931;p=perl%2Fmodules%2FVariable-Temp.git Introduce set_temp(), the non-lvalue variant of temp() --- diff --git a/lib/Variable/Temp.pm b/lib/Variable/Temp.pm index c0e2aef..be72888 100644 --- a/lib/Variable/Temp.pm +++ b/lib/Variable/Temp.pm @@ -55,6 +55,9 @@ Several C calls can be made onto the same variable, and the restore are pr Note that destructors associated with C<$var> will B be called when C 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 5.14, the pseudo-statement C will cause compilation errors on C 5.12.x and below. +If you want your code to run on these versions of C, you are encouraged to use L instead. + =cut sub temp (\$) :lvalue { @@ -64,9 +67,26 @@ sub temp (\$) :lvalue { $$var; } +=head2 C + + set_temp $var; + set_temp $var => $value; + +A non-lvalue variant of L that can be used with any version of C. + +=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 is only exported on request by passing C<'temp'> to the module import list. +The functions L and L 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; =head1 CAVEATS diff --git a/t/01-import.t b/t/01-import.t index a51acb7..2870901 100644 --- a/t/01-import.t +++ b/t/01-import.t @@ -3,12 +3,13 @@ use strict; use warnings; -use Test::More tests => 2 * 1; +use Test::More tests => 2 * 2; require Variable::Temp; my %syms = ( - temp => '\$', + temp => '\$', + set_temp => '\$;$', ); for (sort keys %syms) {