in reply to Dreaming of Post Interpolation

my $text = q{
  Dear $person,
  I know that this text is $adjective.
  But I wish it could be...


my $person    = 'Mom';
my $adjective = 'not interpolated';

print eval "qq{$text}";

Replies are listed 'Best First'.
RE: Re: Dreaming of Post Interpolation
by mdillon (Priest) on May 30, 2000 at 20:52 UTC

    this is the cleanest i've seen yet. the only thing that breaks this is unbalanced brackets, but in a 'sub interpolate', it's easy enough to get around such restrictions.

    on a side note, the Interpolate module doesn't really help much for BBQ's problem. this solution, however, gets perl to do real double-quotish interpolation on an arbitrary scalar value.

      Yeah, the Interpolat module wasn't as good as what it looked like from the examples. It just does a few tricks with hashes, and seems to be directed towards maintainin string formats more than actually doing heavy linkage.

      I think that what I was looking for is a new data state. Something that would cascade through whatever assignments had been done before. My numeric example was probably the best way to demonstrate. Come to think of it, cascade sounds like a cool name. Imagine it:
      cascade $n = 1; my $m = $n*2; my $o = $m*2; my $s = "$n $m $o\n"; print $s; $n = 2; print $s; ^d 1 2 4 2 4 8
      now would THAT be cool? Does anyone know if there a perl ideas/wishlist somewhere?

      # Trust no1!
        Hmm. Seems to me that this could be done with a fancy Tie somehow. Not quite as you have posted it though....Perhaps if $m, $o, and $s were also cascaded...
        How far would you want this to go? While the above example could be done, I can see it getting messy with subroutine calls. For example:
        $n=1; #we have no idea which variables should be cascaded. $subref=sub { return $n *3 }; print &$subref, "\n"; $n=2; print &$subref, "\n";
        Will that output
        Stream of conciousness: the Monitor package uses a tie on a variable to track when a variable is changed...can we track when it is accessed, and turn on cascading for any lvalue, etc? (I see dire consequences on the stack/heap, but...) Then when any such value is modified, you could go through the cascade stack and reset every value. It would go nuts on code like:
        use Cascade; cascade $n; cascade $m; $n=1; $m=$n; $n=$m+1;
        Because $m is altered by $n, and thus when we change $n, we recalc $m, which is in turn set by $n, so we recalc....

        But I guess this isn't much different from screwing your reference count by doing a $a=\$a (except of course that that doesn't hang).

        All speculation anyway, since I don't believe that there is any way to find what lvalue is affected by a variable reference.

        The trouble is, you'd have to completely scrap the way Perl handles assignments.

        For $foo = $bar + 1, the rhs is evaluated first and the result is stored in $foo. What you want is to store the rhs expression in $foo (ie, more or less a sub ref).

        And, as swiftone says, every variable that has a cascaded variable on the rhs of its assignment is forced to become a cascade variable itself, with potentially dire results. Perhaps meltdown would be a better name :)

        this is already possible with tie, although not quite so transparently. however, i'm sure that creative use of 'use overload' could be used to achieve the desired effect.