AnomalousMonk has asked for the wisdom of the Perl Monks concerning the following question:

dHarry's reply to printig with variables in text suggested (at least to my mind) that a HERE-doc could be set up to do an eval-like interpolation of things in a string that look like scalars; i.e., if the string 'all $foo the $bar way' were interpolated from a file into a HERE-doc, some HERE-doc mechanism could then be invoked to further interpolate $foo and $bar (assuming they were already defined in the script) rather than using a subsequent eval step, which is the only way I can see to do this sort of thing.

Exists there such HERE-doc magic, or am I just enjoying a beautiful dream?

Update:

I should have included the sentence dHarry wrote that piqued my interest:

One way to do it is to inline the file in a HEREDOC, the variables will be interpolated. (Maybe not the most elegant solution but I used it to good effect in QED solutions.)

Replies are listed 'Best First'.
Re: HERE-doc two-stage interpolation magic
by tilly (Archbishop) on Aug 06, 2008 at 20:11 UTC
    You are engaged in wishful thinking. The following does interpolation of variables into the text:
    my $foo = "praise"; my $bar = "good"; print <<"HERE"; all $foo the $bar way HERE
    The following does not:
    my $string = 'all $foo the $bar way'; my $foo = "praise"; my $bar = "good"; print <<"HERE"; $string HERE
    If you want multi-level interpolation, you probably want to look into a templating module. Of which there are many on CPAN. (I personally use the template toolkit, but there are many good ones.)
Re: HERE-doc two-stage interpolation magic
by betterworld (Curate) on Aug 06, 2008 at 20:07 UTC

    The interpolation in here-documents has no more in common with eval than has normal interpolation in double-quoted strings: my $var = "this is a string with $values";

    From perlop:

    The terminating string may be either an identifier (a word), or some quoted text. If quoted, the type of quotes you use determines the treatment of the text, just as in regular quoting. An unquoted identifier works like double quotes.

    So, if you don't want variables to interpolated:

    my $string = <<'EOF'; $50 for shipping EOF
OT: nice username
by Your Mother (Archbishop) on Aug 07, 2008 at 01:13 UTC

    Best username yet. Well, top 10, anyway.

      Oh wow, nice catch. I would have to agree.

      <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
        Thank you very much.

        Yeah, that's why I liked it. I read it three times before my brain bothered to parse it.

Re: HERE-doc two-stage interpolation magic
by perrin (Chancellor) on Aug 07, 2008 at 05:00 UTC
    For possible solutions, see perlfaq4 for "How can I expand variables in text strings?"