in reply to Dreaming of Post Interpolation

Eh? That's just filling in a template. You can do it with symbolic refs (if the variables aren't lexicals) or, preferably, with hash lookups.
my $text = q{ Dear $person, I know that this text is $adjective. But I wish it could be... }; my %replace = (person => 'Mom', adjective => 'not interpolated'); $text =~ s/\$(\w+)/$replace{$1}/g; print $text;

Replies are listed 'Best First'.
RE: Re: Dreaming of Post Interpolation
by BBQ (Curate) on May 30, 2000 at 04:24 UTC
    Yes, regex... But would that be considered true interpolation?

    The idea of searching within word boudaries for a string starting with \$ occured to me several times, but I have always thought of it as not being very practical. Let me put it this way (this time with math):
    $n = 1; $m = $n*2; $o = $m*2; $s = "$n $m $o\n"; print $s; $n = 2; print $s; ^d 1 2 4 2 4 8
    Sure this is a simplistic example (as was my previous), but what when you have 1000+ more lines of code, wouldn't it be alot easier to control? Is the only way out to run a regex after assignment? There must be another way... Think of the possibilities!

    Am I suffering from fuzzy-logic syndrome?
      I forgot about the double-e substitution ( ie, s/(\$\w+)/$1/gee ). You can do that with lexicals.

      No matter what, you end up doing some sort of substitution and/or eval. In the second example you want to re-evaluate $m=$n*2 whenever $n is reassigned, which isn't possible without creating more complex data structures. That's far different from interpolating variables into a string.

        I'm sorry, but I don't see why its different at all! As a matter of fact, in both cases, I have variables in a string. The contents of those variables should change later on, but not the string itself! I could agree that there are different approaches to each (basically because on deals with numbers and the other with text) but that doesn't mean that the presented wish changes.

        Had I defined $person and $adjective (with different values) before the string, then both would be practically the same examples.

        I don't think I've done a good job of explaining what I was thinking about, and I am starting to think that interpolation doesn't quite get the idea through. Referencing is closer as a term, but it has already been defined, and I don't beleive it comes close to what I wish perl would do either. Its fustrating to try and explain a concept that doesn't have any good examples (that I can provide at least).

        Nevertheless, thanks for the input!