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

There must be a Perlish way to do this. . . I have a situation where I want to load some text strings from a file for later printing, but the variables that are going to be inserted into the strings are not known until much later in the program. So what I think I want is to find a way to cause the string to re-interpolate the scalars. Here's some sample code:
$message = "Hello $name\n"; #. . . other code that does unrelated stuff . . . $name = "Larry Wall"; print $message;
What I'd like is for the message to print as "Hello Larry Wall", but what I get is "Hello". Eval doesn't seem to work the way I would expect it to. (So I must be expecting incorrectly, too!) I've tried this:
$message = "Hello $name\n"; #. . . other code that does unrelated stuff . . . $name = "Larry Wall"; print eval $message;
I still just get "Hello" back. And then I got to thinking that maybe I got just the "Hello" because $name was already substituted, and by this time only "Hello" exists in the string (without the $name). So, I tried to declare $message with single quotes thusly:
$message = 'Hello $name\n'; #. . . other code that does unrelated stuff . . . $name = "Larry Wall"; print eval $message;
And that didn't work either. I got nuthin.
I tried lots of other stuff that I'm embarassed to admit, and so I won't.
I'm running under the assumption that it is STILL true that "Perl makes easy things easy and hard things possible." I've a feeling that this comes under the first category, rather than the last. But either way, there must be a solution.
So how do I get Perl to re-interpolate the $message string when I need it to?
Thanks much, in advance.
--Mark

Replies are listed 'Best First'.
Re: Postdefining scalars
by japhy (Canon) on Mar 27, 2001 at 04:11 UTC
Re: Postdefining scalars
by japhy (Canon) on Mar 27, 2001 at 04:14 UTC
    On another note, you could use my module DynScalar.
    use DynScalar 'delay'; $greeting = delay { "Hello, $name" }; print "Who are you? "; chomp($name = <STDIN>); print $greeting;


    japhy -- Perl and Regex Hacker
Re: Postdefining scalars
by petral (Curate) on Mar 27, 2001 at 11:37 UTC
    You actually can do what you thought you could and you were close:
    $message = '"Hello $name\n"'; #. . . other code that does unrelated stuff . . . $name = "Larry Wall"; print eval "$message";
    Putting $message in quotes gets you one interpolation, then eval'ing the "H...\n" (with its quotes) gets you another.  ...but as mentioned above (and probably below), there are Better Ways To Do It.

    p
Re: Postdefining scalars
by little (Curate) on Mar 27, 2001 at 12:40 UTC
    You defined the message just before you defined the name, so just do
    #. . . other code that does unrelated stuff . . . $name = "Larry Wall"; $message = "Hello $name\n"; print $message;
    or do like
    $message = "Hello "; #. . . other code that does unrelated stuff . . . $name = "Larry Wall"; $message .= $name."\n"; print $message;

    Have a nice day
    All decision is left to your taste