in reply to Dreaming of Post Interpolation

Here's the module I was working on last night before you found Interpolation.pm. It's harsh, but actually gets the job done, although you have to type your variables as either string or expression. I like the way Interpolation.pm uses import() instead of forcing you to tie your variables yourself, so I borrowed that bit.

Update: Having had a few moments to mull over code generated too late at night, I see it is simpler than I was making it--none of that silly data typing is needed....

package EvalScalar; use Carp; sub import { my $caller_pkg = caller; my $my_pkg = shift; for (@_) { my $var = $_; $var =~ s/^\$//; my $temp; tie $temp, $my_pkg; *{$caller_pkg . '::' . $var} = \$temp; } } sub unimport { my $caller_pkg = caller; my $my_pkg = shift; for (@_) { my $var = $_; $var =~ s/^\$//; my $temp; my $old_var = *{$caller_pkg . '::' . $var}{SCALAR}; *{$caller_pkg . '::' . $var} = \$temp; untie $$old_var; } } sub TIESCALAR { my $pkg = shift; my $type = shift; my $temp = ''; my $self = \$temp; bless $self, $pkg; return $self; } sub STORE { my $self = shift; my $value = shift; $value =~ s/\$(\w+)(?!::)/'$' . caller() . '::' . $1/ge; $$self = $value; } sub FETCH { my $self = shift; my $value = eval $$self; if ($@) { carp $@; return ''; } else { return $value; } } 1;
and the example usage...
#!/usr/bin/perl -w use strict; use EvalScalar qw($a $b $c); # Only works with globals use vars qw($person $num); $num = 3; # Single-quote the rhs if you want # evaluation of the assignment # delayed until read-time. $a = '$num * 2'; $b = '$a + $num'; $c = '"$num, $a, $b"'; for $num (1..5) { print "\$a=$a, \$b=$b, \$c=$c\n"; } $a = '"Dear $person, this sorta works\n"'; for $person qw(Mom Dad BBQ) { print $a; } $a = '"But unqualified subs will be called in EvalScalar namespace" . +foo()'; print $a;