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;

In reply to Re: Dreaming of Post Interpolation by takshaka
in thread Dreaming of Post Interpolation by BBQ

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.